具有多个消费者的生产者使用notify()

时间:2018-05-04 18:50:07

标签: java concurrency producer-consumer

我有这个代码,它是使用单个生产者和共享Q类对象上的多个使用者模拟生产者消费者问题。我在这里使用notify()而不是notifyAll(),因为我需要理解为什么这段代码会陷入死锁或无限等待状态。

我的观点是:如果有一个生产者和多个消费者,那么notify()将只调用处于等待状态的一个线程,其余将保持在wait()状态。然后生产者将再次恢复,因此代码将继续执行。

观察:这里所有线程生产者和消费者都处于无限等待状态。代码如下所示:

public class ProdConsProb {
    public static void main(String[] args) {
        Q q = new Q();
        Thread producerThread = new Thread(new Producer(q), "producerThread");
        Thread consumerThread = new Thread(new Consumer(q), "Consumer1");
        Thread consumerAnotherThread = new Thread(new Consumer(q), "Consumer2");
        Thread consumerYetAnotherThread = new Thread(new Consumer(q), "Consumer3");
        producerThread.start();
        consumerThread.start();
        consumerAnotherThread.start();
        consumerYetAnotherThread.start();
    }
}

class Producer implements Runnable {
    Q q;

    public Producer(Q q) {
        this.q = q;
    }

    @Override
    public void run() {
        int i = 0;
        while (true)
            try {
                q.setN(i++);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

    }

}

class Consumer implements Runnable {
    Q q;

    public Consumer(Q q) {
        this.q = q;
    }

    @Override
    public void run() {
        while (true)
            try {
                q.getN();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}

class Q {
    private int n = 0;
    boolean valueSet = false;

    public synchronized int getN() throws InterruptedException {
        while (!valueSet) 
        {
            wait();
        }
        valueSet = false;
        notify();
        return n;
    }

    public synchronized void setN(int n) throws InterruptedException {
        while (valueSet == true)
        {
            wait();
        }
        this.n = n;
        valueSet = true;
        notify();

    }
}

我添加了一些sysouts。生成的日志如下所示:

producerThread :: SetN : Valueset is false
producerThread :: Producer inserted 0
producerThread :: SetN : Valueset after is true
producerThread :: SetN : Valueset is true
producerThread wait() ------------ Active 
producerThread :: SetN :Wait() Valueset is true
Consumer3  Start :: GetN : Valueset is true
Consumer3 :: Consumer read 0
Consumer3  End :: GetN : Valueset after is false
Consumer3  Start :: GetN : Valueset is false
Consumer3 wait() ------------ Active 
Consumer3 :: GetN :Wait() Valueset is false
Consumer2  Start :: GetN : Valueset is false
Consumer2 wait() ------------ Active 
Consumer2 :: GetN :Wait() Valueset is false
Consumer1  Start :: GetN : Valueset is false
Consumer1 wait() ------------ Active 
Consumer1 :: GetN :Wait() Valueset is false
producerThread wait()   ------------- left 
producerThread :: Producer inserted 1
producerThread :: SetN : Valueset after is true
producerThread :: SetN : Valueset is true
producerThread wait() ------------ Active 
 -->>   producerThread :: SetN :Wait() Valueset is true
        Consumer3 wait() left 
        Consumer3 :: Consumer read 1
        Consumer3  End :: GetN : Valueset after is false
        Consumer3  Start :: GetN : Valueset is false
        Consumer3 wait() ------------ Active 
        Consumer3 :: GetN :Wait() Valueset is false
 ????   Consumer2 wait() left 
        Consumer2 wait() ------------ Active 
        Consumer2 :: GetN :Wait() Valueset is false
这里奇怪的是一旦生产者在插入1后通知,消费者3读取数据并通知生产者。现在,生产者3必须从wait()中触发,但customer2线程将离开wait()并返回wait()。

注意:此代码适用于notifyAll(),但我正在寻找一个原因,为什么它失败了notify()。

1 个答案:

答案 0 :(得分:2)

它失败了,因为生产者和消费者正在等待同一个监视器,而内部锁不支持单独的条件。如果发生通知,则生产者或消费者可以获得该通知。但是给定的通知仅适用于其中一个。当一个人收到通知,只有另一个人可以采取行动时,通知没有任何好处:通知的线程醒来,发现它正在等待的条件仍然是假的,然后返回等待。

如果你看看ArrayBlockingQueue,它是用ReentrantLock实现的,具有单独的条件,一个用于消费者,一个用于生产者,因此这种bug不会发生。