【问题标题】:How deadlock and starvation problems occur in java producer consumer pattern.?java生产者消费者模式中如何发生死锁和饥饿问题。?
【发布时间】:2020-03-07 14:38:57
【问题描述】:

我编写了以下显示生产者消费者模式的 java 代码。我想知道生产者消费者模式是如何发生死锁和饥饿的。我在互联网上搜索了这个查询。但我找不到合适的文章来清楚地解释生产者消费者模式是如何发生死锁和饥饿的。

public class InterThreadCommunication_Producer_Consumer {


        static Queue<Integer> queue = new LinkedList<>();
        static int size = 4;




       public static void produce() throws InterruptedException {

                int value = 0;

                while(true) {


                    synchronized (queue) {


                        while(queue.size() >= size) {
                            queue.wait();
                        }


                        queue.add(value);


                        System.out.println("Produced" + value);

                        value++;


                        queue.notify();


                        Thread.sleep(1000);

                    }



                }


            }


            public static void consume() throws InterruptedException {

                while(true) {


                    synchronized (queue) {



                    while(queue.isEmpty()) {
                        queue.wait();
                    }


                    int value = queue.poll();

                    System.out.println("Consume" + value);

                    queue.notify();

                    Thread.sleep(1000);
                }

                }



            }














        public static void main(String[] args) throws InterruptedException {

            Thread producerThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        produce();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            Thread consumerThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                       consume();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            producerThread.start();
            consumerThread.start();
            producerThread.join();
            consumerThread.join();

        }

    }

【问题讨论】:

    标签: java multithreading


    【解决方案1】:

    你有死锁吗?

    死锁并非特定于生产者-消费者等协作模式,而是来自锁定模式。拥有一把锁,您就安全了。

    对于死锁,您至少需要两个锁和两个线程:

    Time      0       1
    Thread 1: Lock(A) Wait-Lock(B) 
    Thread 2: Lock(B) Wait-Lock(A)
    

    如您所见,在时间 0,两个线程都在获取对两个资源的锁定。在时间 1,他们正在互相等待,没有非合作的可能性。

    通知政策

    以您的示例为例:您在具有相同通知(queue 对象)的两个不同线程中使用wait()notify(),这表明通知不属于明确事件(即queue非空)。这会导致问题,尤其是当您使用超过 2 个线程时。您将相同的通知重复用于:

    1. 可用数据
    2. 队列空间可用

    通知您在不同对象中等待的条件。我还建议缩小关键部分(同步部分)的范围。

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 1970-01-01
      • 2013-03-06
      • 2011-05-04
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 2012-07-27
      • 2012-05-21
      相关资源
      最近更新 更多