0

我正在尝试只有一个条目的 int itemHolder 的消费者生产者问题。我不知道为什么消费者线程在放置项目时没有通知生产者线程,预期的行为是消费者线程等待生产者将项目放入 itemHolder 中。另一方面,当我对外部互斥对象使用锁定时,它可以完美运行。

public class ProducerConsumer {

    public static void main(String... args) {
        new ProducerConsumer().execute();
    }

    private volatile int itemHolder = -1; // -1 value represent that ItemHolder is empty

    private void execute() {
        final Thread producer = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i < 5; i++) {
                    synchronized (this){
                        while (itemHolder != -1){ // ItemHolder is full
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        itemHolder = i;
                        notify();
                        System.out.println(String.format("producer: ItemHolder has value, Consumer notified..."));

                    }
                }

            }


        }, "Producer-thread");

        final Thread consumer = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    synchronized (producer){
                        try {
                            while (itemHolder == -1){ // Don't consume if itemHolder don't have a value
                                producer.wait();
                            }
                            System.out.println(String.format("CONSUMER: consuming %s...", itemHolder));
                            itemHolder = -1;    // re-initialize the itemHolder
                            producer.notify();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        }, "Consumer-thread");

        consumer.start();
        producer.start();

    }

锁定外部互斥体 这可以按预期正常工作。

public class ProducerConsumerWithMutex {

    public static void main(String... args) {
        new ProducerConsumerWithMutex().execute();
    }
    private final String mutex = "";
    private volatile int itemHolder = -1;

    private void execute() {
        final Thread producer = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i < 5; i++) {
                    synchronized (mutex){
                        while (itemHolder != -1){ // itemHolder is full
                            try {
                                mutex.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        itemHolder = i;
                        System.out.println(String.format("producer: producing %s...", i));
                        mutex.notify();
                        System.out.println(String.format("producer: Consumer notified, itemHolder has item..."));

                    }
                }

            }


        }, "Producer-thread");

        final Thread consumer = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    synchronized (mutex){
                        try {
                            while (itemHolder == -1){
                                System.out.println("CONSUMER: itemHolder is empty, waiting...");
                                mutex.wait();
                            }
                            System.out.println(String.format("CONSUMER: consuming %s...", itemHolder));
                            itemHolder = -1;
                            mutex.notify();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        }, "Consumer-thread");

        consumer.start();
        producer.start();

    }
4

3 回答 3

4

因为在第一个生产者中,您同步的thisRunnable,而不是生产者本身。

因此,您需要synchronized(producer)改用它,除非它不会编译,因为producer该行上尚不可用。

或者,您可以将 Runnable 命名为:

Runnable producerRunnable = ...; //synchronized on this

在您的消费者中:

synchronized(producerRunnable) {...}

但是您使用单独的互斥锁的第二种方法更可取,除了锁定""是一个非常糟糕的主意,因为这是一个全局常量(空字符串在字符串池中)。你应该更喜欢这样的东西:

private final Object mutex = new Object();
于 2013-06-02T17:03:53.810 回答
1

当两个线程都使用相同的对象/类锁时,等待通知起作用。在您的情况下,用于等待/通知的锁与此处提到的不同:

synchronized(producer) // lock on producer object

synchronized(this) // Runnable object.
于 2013-06-02T17:11:01.117 回答
0

在 supportig assylias 的回答之后,这里是另一种选择。

private Object lock = new Object();

锁定

synchronized (lock){
      lock.wait();
}

开锁

synchronized (lock){
      lock.notify();
}
于 2013-06-02T17:09:45.233 回答