1
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 %d\n", 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 %d\n", 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();
        }
    }
};

另外为什么异常包含null?这是来自http://www.cs.rice.edu/~wns1/papers/2006-PPoPP-SQ.pdf清单 3的实现

我得到输出

hello from Java
I am producer number 1
null
null

为什么我会得到空值?

4

2 回答 2

4

您尚未在 main 方法中启动队列。我猜你得到一个 NullPointerException 因为队列对象从未被创建并且生产者和消费者引用了为空的队列。

于 2012-06-18T20:29:10.940 回答
0

即使您正确初始化队列,您的实现仍然存在重大问题。如果Consumer线程在队列为空时尝试获取一个项目(根据您的代码这是完全可能的),那么Consumer线程进入无限期等待,持有对该对象的锁定。Producer线程永远不能项目放入队列。整个事情都会停止。

于 2012-06-18T20:46:10.280 回答