等待并通知Java中的死锁情况

时间:2017-02-17 07:24:33

标签: java concurrency

  1. 线程t1在wait()被击中后进入死锁状态。即使 t2中有notify()。代码陷入僵局。 没有得到Print语句 - “等待被释放后::::”

  2. 我可以看到两个线程竞争在counterAdd()中获取监视器。因此,我认为通知将起作用。

    package com.java.thread.practice;
    
      public class WaitAndNotify4 {
    
            int counter = 0;
    
            /*
               CounterAdd() is to be accessed by both t1 and t2.
               If not synchronized not giving consistent output. 
            */ 
            synchronized int counterAdd(){
                return counter++;
            }
    
            public static void main(String[] args) throws InterruptedException{
    
                // Creating method to call the threads.
                WaitAndNotify4 andNotify4 = new WaitAndNotify4();
                andNotify4.testRaceCondition();
    
            }
    
            private  void testRaceCondition() throws InterruptedException {
    
                // Thread t1 created and launched.
    
                Thread t1 = new Thread(new Runnable(){
                    @Override
                    public void run() {
    
                        for(int i=0; i<5; i++){
                            synchronized(this){
                            if(i== 1){
                                System.out.println("Calling wait after count 1");
                                try {
                                 // Assuming that this wait will be resumed by notify in t2.
    
                                    wait();
                                    System.out.println("After wait is released :::: ");
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
    
                            }
                            }
                            counterAdd();
                        }
                    }
                });
    
    
                Thread t2 = new Thread(new Runnable(){
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
    
                        for(int i=0; i<5; i++){
                            if(i==2){
    
                                synchronized(this){
                                    System.out.println("Before releasing the counter :::::");
                                    notify();
                                    System.out.println("After releasing the counter :::::");
                                }
    
                            }
                            counterAdd();
                        }
    
                    }
                });
    
    
    
                t1.start();
                t2.start();
    
                t1.join();
                t2.join();
    
                System.out.println(" Sum value is found as ::::: "+counter);
    
            }
        }
    

1 个答案:

答案 0 :(得分:1)

您正在同步不同的对象。对象t1的第一种情况,t2的第二种情况,counterAdd上的方法andNotify4。为了锁定andNotify4,你需要做任何类似的事情。

public class Main {
    private int counter = 0;

    synchronized int counterAdd() {
        return counter++;
    }

    public static void main(String[] args) throws InterruptedException {
        Main andNotify4 = new Main();
        andNotify4.testRaceCondition();
    }

    private void testRaceCondition() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    synchronized (Main.this) {
                        if (i == 1) {
                            System.out.println("Calling wait after count 1");
                            try {
                                Main.this.wait();
                                System.out.println("After wait is released :::: ");
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    counterAdd();
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    if (i == 2) {
                        synchronized (Main.this) {
                            System.out.println("Before releasing the counter :::::");
                            Main.this.notify();
                            System.out.println("After releasing the counter :::::");
                        }
                    }
                    counterAdd();
                }
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(" Sum value is found as ::::: " + counter);
    }
}