【问题标题】:A question about Producer-Consumer Model in Java关于Java生产者-消费者模型的问题
【发布时间】:2020-07-24 02:55:15
【问题描述】:

我编写了一个 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)

【问题讨论】:

  • 永远不要在同步语句或方法中调用 Thread.sleep()。

标签: java multithreading producer-consumer


【解决方案1】:

当我运行您的代码时,出现以下错误:

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

引发该异常的代码行是对方法 wait() 的调用。
您正在调用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();
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多