为什么不在这里工作?

时间:2012-06-18 20:15:40

标签: java concurrent-programming java.util.concurrent

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

为什么我会变空?

2 个答案:

答案 0 :(得分:4)

您尚未在main方法中启动队列。我猜你得到一个NullPointerException,因为永远不会创建队列对象,而Producer和Consumer引用的队列是null。

答案 1 :(得分:0)

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