class NaiveSQ<E> {
boolean putting = false;
E item = null;
public synchronized E take() throws InterruptedException {
while (item == null)
wait();
E e = item;
item = null;
notifyAll();
return e;
}
public synchronized void put (E e) throws InterruptedException {
if (e == null)
return;
while (putting)
wait();
putting = true;
item = e;
notifyAll();
while (item != null)
wait();
putting = false;
notifyAll();
}
}
class Producer implements Runnable {
int id = -1;
int limit = 1;
Producer(int x) {
id = x;
}
public void run() {
System.out.printf("I am producer number %dn", id);
for (int i=0; i<limit; i++) {
Integer I = new Integer(i);
try {
Test.queue.put(I);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
class Consumer implements Runnable {
int id = -1;
Consumer(int x) {
id = x;
}
public void run() {
try {
Integer I = Test.queue.take();
System.out.printf(
"I am consumer number %d - I read %dn", id, I.intValue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public class Test{
static NaiveSQ<Integer> queue;
public static void main (String [] args){
System.out.println("hello from Java");
Thread p = new Thread(new Producer(1));
p.start();
for (int i=0; i<1; i++) {
Thread c = new Thread(new Consumer(i));
c.start();
}
}
};
另外,为什么异常包含空?这是清单 3 http://www.cs.rice.edu/~wns1/papers/2006-PPoPP-SQ.pdf 实现
我得到的输出为
hello from Java
I am producer number 1
null
null
为什么我得到空?
您尚未在 main 方法中初始化队列。我猜你会得到一个 NullPointerException,因为队列对象永远不会被创建,并且生产者和消费者引用了空的队列。
即使您正确初始化了队列,您的实现仍然存在一个主要问题。如果Consumer
线程在队列为空时尝试获取项目(根据您的代码,这是完全可能的),则线程Consumer
无限期等待,保持对对象的锁定。 线程Producer
永远无法将项目放入队列。整个事情将停止。