简体   繁体   English

JAVA使用者-生产者多线程应用程序-代码流

[英]JAVA consumer-producer multi-threaded application - flow of code

I was practicing this famous application and have a question. 我正在练习这个著名的应用程序,并且有一个问题。 I found there are 4000 Q/A on this topic in this site, but none of them is related to this point and hence asking this question. 我发现此站点上有4000个有关此主题的Q / A,但是它们都与这一点无关,因此提出了这个问题。

Here is the simple code - 这是简单的代码-

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);
    }
}

And here is the output - 这是输出-

#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

So here is the flow of code - 所以这是代码流-

#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?

Am I missing something simple? 我是否缺少简单的东西? Can you please explain? 你能解释一下吗?

Many many thanks in advance. 非常感谢。

You asked 您询问

3 So now code should go back to the Producer thread. 3现在,代码应返回到Producer线程。 But why Consumer is entering it's own wait() block at LINE 3? 但是,为什么消费者要在第3行输入它自己的wait()块?

well consumer thread will continue to run after it makes available=false and notify the other thread. 好消费者线程将在available=false后继续运行,并通知另一个线程。 Till producer thread does not make available=true it will call wait and blocked. 直到生产者线程不可用= true,它将调用wait并被阻止。

Best way to implement the producer/consumer pattern is through BlockingQueue 实现生产者/消费者模式的最佳方法是通过BlockingQueue

Here is the example 这是例子

I don't see how your threads are coordinating their activity. 我看不到您的线程如何协调其活动。 When the Producer is sleeping it isn't waiting for resource. 当生产者在睡觉时,它并不在等待资源。 When the Consumer calls resource.notify() it is possible that no Producer is waiting for the resource. 当使用者调用resource.notify()时,可能没有生产者正在等待资源。

As @ M Sach has already pointed out, the Consumer thread is free to continue processing. 正如@ M Sach已经指出的那样,使用者线程可以自由继续处理。

At this point the two threads are not coordinating with each other via resource, so the observed behaviour seems to be as expected from a running Consumer and a sleeping Producer. 此时,两个线程尚未通过资源相互协调,因此观察到的行为似乎与运行中的使用者和睡眠中的生产者所期望的一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM