我的Producer Consumer代码正在死锁

时间:2017-02-15 22:35:55

标签: java multithreading

代码陷入僵局。即使它一旦到达wait()就要进入制作人部分,它也会陷入僵局。根据我的理解,如果wait()被击中,它应该转到消费者线程而不是陷入死锁。

package com.java.thread.self.practice;

public class Producer_Consumer {

        private volatile boolean prodFlag = true;
        private volatile boolean consFlag = false;

        public static void main(String[] args){

            Producer_Consumer producer_Consumer = new Producer_Consumer();
            producer_Consumer.startThreads();

        }

        private void startThreads() {

            Thread producer = new Thread(new Runnable(){

                @Override
                public void run() {
                    while(true){
                        try {
                            System.out.println("Before Producer invocation :::::: ");
                            producer();
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }

            });



            Thread consumer = new Thread(new Runnable(){

                @Override
                public void run() {
                    while(true){
                        try {
                            System.out.println("Before Consumer invocation :::::: ");
                            consumer();
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }

            });


            producer.start();
            consumer.start();
        }

         void producer() throws InterruptedException {

             System.out.println("Inside the producer method ::::: "+this.getClass());

             synchronized(this){

                 if(prodFlag){

                     System.out.println("PRODUCE !!!");
                     consFlag = true;
                     System.out.println("Before calling wait in producer :::::: ");
                     notify();
                     wait();
                     System.out.println("After calling wait in producer :::::: ");

                 }else{

                     System.out.println("Before calling notify in producer :::::: ");
                     consFlag = true;
                     wait();
                     System.out.println("After calling notify in producer :::::: ");

                 }


             }
        }

         void consumer() throws InterruptedException {

             System.out.println("Inside the consumer method ::::: "+this.getClass());

             synchronized(this){

                 if(consFlag){

                     System.out.println("CONSUME !!!");
                     prodFlag = true;
                     System.out.println("Before calling wait in consumer :::::: ");
                     notify();
                     wait();
                     System.out.println("After calling wait in consumer :::::: ");

                 }else{

                     System.out.println("Before calling notify in consumer :::::: ");
                     prodFlag = true;

                     wait();

                     System.out.println("After calling wait in consumer :::::: ");

                 }

             }
        }
}

2 个答案:

答案 0 :(得分:2)

当你这样做时

synchronized(this)
你锁定了整个班级。它将被锁定,直到该代码块结束。由于您声明了标记volatile,因此不需要显式同步。

在这种情况下,您根本不需要发出wait()notify信号。但是,如果你想拥有一些原子业务逻辑,你需要重新编写代码,以便不用你的类作为密钥来阻止整个大块。

答案 1 :(得分:1)

您的程序不会陷入死锁 - 无论是在运行时还是在调试时。我想你不熟悉调试多个线程,对吧?也许在你看来,当你必须切换到消费线程并继续调试时,你的生产线程处于死锁状态?