JAVA消费者-生产者多线程应用程序——代码流程

标签 java multithreading

我正在练习这个著名的应用程序并有一个问题。我发现这个网站上有 4000 个关于这个主题的问答,但没有一个与这一点相关,因此提出这个问题。

这是简单的代码-

class Resource {
    int contents;
    boolean available = false;
}

class Producer implements Runnable {
    Thread t;
    private Resource resource;

    public Producer(Resource c) {
        resource = c;
        t=new Thread(this);
        t.start();
    }

    public void run() {
        for (int i = 0; i < 3; i++) {
            synchronized (resource) {
                while (resource.available == true) {
                    //System.out.println("Producer -> calling wait");
                    try {
LINE 1                  resource.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } 
                }
LINE 2          resource.contents = i;
                resource.available = true;
                //System.out.println("Producer -> calling notify");
                resource.notify();
                System.out.println("Producer " + " put: " + resource.contents);
                try {
                    Thread.sleep((int)(Math.random() * 100));
                } catch (InterruptedException e) { }
            }
        }
    }
}

class Consumer implements Runnable {
    Thread t;
    private Resource resource;

    public Consumer(Resource c) {
        resource= c;
        t=new Thread(this);
        t.start();
    }

    public void run() {
        for (int i = 0; i < 3; i++) {
            synchronized (resource) {
                while (resource.available == false) {
                    System.out.println("Consumer -> calling wait");
                    try {
LINE 3                  resource.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
LINE 4          resource.available = false;
                //System.out.println("Consumer -> calling notify");
                resource.notify();
                System.out.println("Consumer " + " got: " + resource.contents);
            }
        }
    }
}

public class ProducerConsumerTest {
    public static void main(String[] args) {
        Resource c = new Resource ();
        Producer p1 = new Producer(c);
        Consumer c1 = new Consumer(c);
    }
}

这是输出 -

#1 Producer  put: 0
#2 Consumer  got: 0
#3 Consumer -> calling wait
#4 Producer  put: 1
#5 Consumer  got: 1
#6 Consumer -> calling wait
#7 Producer  put: 2
#8 Consumer  got: 2

这是代码流程 -

#1 Since available=false, Producer will go to LINE 2, make available=true and notify the other thread.
#2 Since available=true, Consumer will go to LINE 4, make available=false and notify the other thread.
#3 So now code should go back to the Producer thread. But why Consumer is entering it's own wait() block at LINE 3?

我错过了一些简单的事情吗?能解释一下吗?

提前非常感谢。

最佳答案

你问了

3 So now code should go back to the Producer thread. But why Consumer is entering it's own wait() block at LINE 3?

消费者线程在使 available=false 并通知其他线程后将继续运行。直到生产者线程没有使 available=true 为止,它将调用 wait 并阻塞。

实现生产者/消费者模式的最佳方式是通过 BlockingQueue

这是example

关于JAVA消费者-生产者多线程应用程序——代码流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24711819/

相关文章:

c++ - 使用 C++ 的递归线程使资源暂时不可用

c++ - memcpy 的多线程编程

c# - Monitor.TryEnter 不起作用

java - 黑莓 - 线程没有响应

java - 如何列出 Maven 项目缺少的依赖项

java - 在 Java 中的双显示器配置上保持 jframe 打开

java - 如何对传出的 SOAP 消息应用 XML 签名

java - netbeans java 检查本地服务是否正在运行

c++进程cpu使用率跳转导致检测

java - 在 GridBagLayout 中布局组件不符合预期