一道面试题,我不会呵呵。各位帮忙看下 请用Java语言实现一个队列,该队列可在多线程环境下使用,多个线程可并发向队列插入数据或读取数据。另外再给出访问这个队列的多线程实现类。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用ConcurrentLinkedQueue不就完了么,难道让自己实现? 题目都没看明白,我承认我弱爆了.....java仍未成功,吾要更加努力! import java.util.ArrayList;import java.util.List;import java.util.Random;public class Queue { List<Integer> Integers = new ArrayList<Integer>(); int maxSize = 4; int count = 0; public Queue() { } public synchronized void add(Integer e) { if (Integers.size() == maxSize) { try { this.wait(); } catch (InterruptedException e1) { e1.printStackTrace(); } } this.notifyAll(); if (Integers.size() < maxSize) { System.out.println(Thread.currentThread().getName() + " " + e); Integers.add(e); count++; } } public synchronized void remove() { if (Integers.size() == 0) { try { this.wait(); } catch (InterruptedException e1) { e1.printStackTrace(); } } this.notifyAll(); if (Integers.size() > 0) { System.out.println(Thread.currentThread().getName() + " " + this.Integers.get(0)); Integers.remove(0); } } public static void main(String args[]) { Queue q = new Queue(); Create c = new Create(q); PullOut po = new PullOut(q); new Thread(c).start(); new Thread(po).start(); }}class Create implements Runnable { String name = "生产者"; private Queue q; public Create(Queue q) { this.q = q; } @Override public void run() { int count = 0; Random r = new Random(); while (count < 10) { int number = r.nextInt() % 20; q.add(number); try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } count++; } }}class PullOut implements Runnable { String name = "消费者"; private Queue q; public PullOut(Queue q) { this.q = q; } @Override public void run() { int count = 0; while (count < 10) { q.remove(); try { Thread.sleep(800); } catch (InterruptedException e) { e.printStackTrace(); } count++; } }} Java字符串缓冲区是什么意思,具体,有兴趣的来? 高分悬赏Java调试工具^_^ 这个需求觉得挺常见的,请问大家! 小小问题请大家帮忙解决 java编译器检查内容(编译原理问题,高手请进) 运用java 程序实现关机 如何用EDITPLUS编译和运行JAVA 分享一个图片分割器源码。。。。一起交流学习!! 关于domino里JDK版本低的问题 java怎样得到ftp服务器一的目录下的文件类表 jdbc执行带参数的SQL语句 JButton改变Icon的问题
java仍未成功,吾要更加努力!
import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class Queue {
List<Integer> Integers = new ArrayList<Integer>();
int maxSize = 4;
int count = 0; public Queue() {
} public synchronized void add(Integer e) {
if (Integers.size() == maxSize) {
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
this.notifyAll();
if (Integers.size() < maxSize) {
System.out.println(Thread.currentThread().getName() + " " + e);
Integers.add(e);
count++;
}
} public synchronized void remove() {
if (Integers.size() == 0) {
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
this.notifyAll();
if (Integers.size() > 0) {
System.out.println(Thread.currentThread().getName() + " "
+ this.Integers.get(0));
Integers.remove(0);
}
} public static void main(String args[]) {
Queue q = new Queue();
Create c = new Create(q);
PullOut po = new PullOut(q);
new Thread(c).start();
new Thread(po).start();
}
}class Create implements Runnable {
String name = "生产者";
private Queue q; public Create(Queue q) {
this.q = q;
} @Override
public void run() {
int count = 0;
Random r = new Random();
while (count < 10) {
int number = r.nextInt() % 20;
q.add(number);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}class PullOut implements Runnable {
String name = "消费者";
private Queue q; public PullOut(Queue q) {
this.q = q;
} @Override
public void run() {
int count = 0;
while (count < 10) {
q.remove();
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}