生产者消费者程序在Java中使用wait

生产者消费者程序在Java中使用wait

本文介绍了生产者消费者程序在Java中使用wait()和notify()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用低级同步和wait()以及notify()在Java中执行经典的Producer-Consumer问题。我知道有更好的实现使用java.util.concurrent包中的结构,但我的问题围绕低级实现:

I am doing classic Producer-Consumer problem in Java using low level synchronization and wait() and notify(). I know there are better implementations using structures from java.util.concurrent package but my problem revolves around low level implementation:

private static ArrayList<Integer> list = new ArrayList<Integer>();

    static Object obj = new Object();

    public static void producer() throws InterruptedException {
        synchronized (obj) {
            while (true) {
                if (list.size() == 10) {
                    System.out.println("Queue full.. Waiting to Add");
                    obj.wait();
                } else {
                    int value = new Random().nextInt(100);
                    if (value <= 10) {
                        Thread.sleep(200);
                        System.out.println("The element added was : " + value);
                        list.add(value);
                        obj.notify();
                    }
                }
            }
        }

    }

    public static void consumer() throws InterruptedException {
        synchronized (obj) {
            while (true) {
                Thread.sleep(500);
                if (list.size() == 0) {
                    System.out.println("Queue is empty...Waiting to remove");
                    obj.wait();
                } else {
                    System.out.println("The element removed was : "
                            + list.remove(0));
                    obj.notify();
                }
            }
        }

    }

程序中有2个线程,具体为生产者和消费者各1个。代码工作得很好。

There are 2 threads in the program, 1 each for producer and consumer specifically. The code works just fine.

唯一的问题是生产者继续产生消息,直到达到最大值(直到列表的大小为10)和消费者一次消耗所有10个。

The only issue is that producer goes on to produce messages until the maximum at once (until size is 10 for the list), and consumer consumes all 10 at once.

如何让生产者和消费者同时工作?

How can I make producer and consumer work at the same time?

这是样本输出:

The element added was : 4
The element added was : 0
The element added was : 0
The element added was : 4
The element added was : 3
The element added was : 1
The element added was : 10
The element added was : 10
The element added was : 3
The element added was : 9
Queue full.. Waiting to Add
The element removed was : 4
The element removed was : 0
The element removed was : 0
The element removed was : 4
The element removed was : 3
The element removed was : 1
The element removed was : 10
The element removed was : 10
The element removed was : 3
The element removed was : 9
Queue is empty...Waiting to remove

编辑:以下是更正后的代码:

Here is the corrected code:

private static ArrayList<Integer> list = new ArrayList<Integer>();
    private static Object obj = new Object();

    public static void producer() throws InterruptedException {
        while (true) {
            Thread.sleep(500);
            if (list.size() == 10) {
                System.out.println("Waiting to add");
                synchronized (obj) {
                    obj.wait();
                }
            }
            synchronized (obj) {
                int value = new Random().nextInt(10);
                list.add(value);
                System.out.println("Added to list: " + value);
                obj.notify();
            }
        }
    }

    public static void consumer() throws InterruptedException {
        while (true) {
            Thread.sleep(500);
            if (list.size() == 0) {
                System.out.println("Waiting to remove");
                synchronized (obj) {
                    obj.wait();
                }
            }
            synchronized (obj) {
                int removed = list.remove(0);
                System.out.println("Removed from list: " + removed);
                obj.notify();
            }
        }
    }


推荐答案

您无法在具有相同对象的同步块中运行两个线程。当一个方法运行时,另一个方法无法运行,直到另一个线程调用等待方法。

You can not run two thread in synchronized block with same object. When one method is running another method can not run until another thread call wait method.

要解决这个问题你应该只在同步块中添加添加删除。有关详细信息,请参阅。

To solve this problem you should just put add and remove in synchronized block. For more information see this.

这篇关于生产者消费者程序在Java中使用wait()和notify()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:00