关于 Java 中生产者-消费者模型的问题

A question about Producer-Consumer Model in Java

我写了一个Java程序来解决多线程中的生产者消费者问题。但它不能正常工作。 节目:

public class ConsumerAndProducer {
static int  products = 0;
static int capacity = 10;
public static void main(String[] args) {
    new Thread(new Producer()).start();
    new Thread(new Consumer()).start();
}
static class Consumer implements Runnable{

    public void consume() {
        synchronized (ConsumerAndProducer.class){
            if(products <= 0){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            products--;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Consumer, remain:" + products);
            if(products == 9){
                notify();
            }
        }
    }

    @Override
    public void run() {
        while(true){
            consume();
        }
    }
}
static class Producer implements Runnable{

    public void produce() {
        synchronized (ConsumerAndProducer.class){
            if(products == capacity){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            products++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Producer, remain:" + products);
            if(products == 1){
                notify();
            }
        }
    }

    @Override
    public void run() {
        while(true){
            produce();
        }
    }
}

错误:

Producer, remain:1
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at zhousai.ConsumerAndProducer$Producer.produce(ConsumerAndProducer.java:69)
    at zhousai.ConsumerAndProducer$Producer.run(ConsumerAndProducer.java:77)
    at java.lang.Thread.run(Thread.java:748)
Consumer, remain:0
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:502)
    at zhousai.ConsumerAndProducer$Consumer.consume(ConsumerAndProducer.java:22)
    at zhousai.ConsumerAndProducer$Consumer.run(ConsumerAndProducer.java:43)
    at java.lang.Thread.run(Thread.java:748)

当我运行你的代码时,我得到了以下错误:

Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread is not owner

抛出该异常的代码行是对方法 wait().
的调用 您正在调用 class Producer 的方法 wait(),但您正在 ConsumerAndProducer.class 上进行同步。 wait() 方法必须在您正在同步的对象上调用,因为该对象拥有锁,您必须在拥有锁的对象上调用 wait()。因此出现错误消息:current thread not owner.

最简单的解决方案是更改您的代码,这样您就可以调用 ConsumerAndProducer.class.wait() 而不仅仅是 wait().

这是你的代码和我建议的修复:

public class ConsumerAndProducer {
    static int  products = 0;
    static int capacity = 10;
    public static void main(String[] args) {
        new Thread(new Producer()).start();
        new Thread(new Consumer()).start();
    }

    static class Consumer implements Runnable {
        public void consume() {
            synchronized (ConsumerAndProducer.class){
                if (products <= 0) {
                    try {
                        ConsumerAndProducer.class.wait(); // change here
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                products--;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Consumer, remain:" + products);
                if(products == 9){
                    ConsumerAndProducer.class.notify(); // change here
                }
            }
        }

        @Override
        public void run() {
            while(true){
                consume();
            }
        }
    }

    static class Producer implements Runnable{
        public void produce() {
            synchronized (ConsumerAndProducer.class){
                if (products == capacity) {
                    try {
                        ConsumerAndProducer.class.wait(); // change here
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                products++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Producer, remain:" + products);
                if(products == 1){
                    ConsumerAndProducer.class.notify(); // change here
                }
            }
        }

        @Override
        public void run() {
            while(true){
                produce();
            }
        }
    }
}