Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA消费生产者多线程应用程序-代码流_Java_Multithreading - Fatal编程技术网

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

JAVA消费生产者多线程应用程序-代码流,java,multithreading,Java,Multithreading,我正在练习这个著名的应用程序,我有一个问题。我发现在这个网站上有4000个关于这个话题的问答,但是没有一个与这一点相关,因此提出了这个问题 下面是简单的代码- class Resource { int contents; boolean available = false; } class Producer implements Runnable { Thread t; private Resource resource; public Produce

我正在练习这个著名的应用程序,我有一个问题。我发现在这个网站上有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 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所以现在代码应该返回到生产者线程。但为什么是消费者 是否在第3行输入自己的wait()块

消费者线程将在它使
可用=false
并通知另一个线程后继续运行。直到producer thread not make available=true,它将调用wait并阻塞

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


这里是

我看不出你的线程是如何协调它们的活动的。当制作人睡觉时,它不会等待资源。当使用者调用resource.notify()时,可能没有生产者在等待资源

正如@已经指出的,消费者线程可以自由地继续处理


此时,两个线程没有通过资源相互协调,因此观察到的行为似乎与正在运行的消费者和正在睡眠的生产者的预期一样。

Hi Sach-非常感谢您的回答。所以关键是使用者线程将继续运行-它将在其块内来回遍历,直到调用wait(),然后控件将转到另一个线程。我错过了这一点。我这边干杯!嗨,里奇,非常感谢你的回答。是的,代码并不完美,我只是从一些网站上了解到,以便于理解。我同意,在一个线程调用notify()之后,线程之间没有这种通信。正如我所评论的,我理解了线程将继续运行的真正意义。我试图让两个答案都被接受,但不幸的是我只能做一个!谢谢你!嗨,我又糊涂了!请原谅。在这种情况下,当代码到达第4行时,为什么代码会再次返回第3行?为什么使用者线程将继续运行?如果我删除(inti=0;I<3;I++)的
行,即使执行第3行也是如此。我没有真正理解这一点-如果您能解释的话,我将非常感谢。第4行设置resource.available=false,因此下次消费者运行for循环时,它将转到第3行,除非制作人同时设置resource.available=true。由于生产者线程中的睡眠,resource.available可能仍然为false。删除for行后,调用第3行的一种方法是,如果消费者线程排在生产者线程之前。resource.available的初始值为false,因此即使没有循环,执行也可以到达第3行。这又回到了我最初的观点:线程不协调它们的活动——至少不是所有可能的执行路径。
#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?