Deadlocks using wait and notify

别等时光非礼了梦想. 提交于 2020-01-13 10:23:32

问题


I am trying to understand how deadlocks are created. I've understood that by using two threads on two synchronized methods, a deadlock can be created. Went through many examples from the net.

Can a deadlock be created with wait and notify? Every time a thread is on wait, it will be notified. So how does this end up in a deadlock?

Illustration of an example will be helpful.


回答1:


Deadlock is caused when two threads try to obtain the same, multiple locks in different order:

    // T1
    synchronized (A) {
      synchronized (B) {
        // ...
      }
    }

    // T2
    synchronized (B) {
      synchronized (A) {
        // ...
      }

}

The only way to prevent deadlocks is to make sure that all threads obtain locks in the same order--either they all do A then B, or they all do B then A.

If you don't have multiple locks, then you don't have a deadlock. However, you can get thread starvation or other things that may look similar to deadlock.




回答2:


A thread which is on wait will not be notified unless some code explicitly notifies it. Therefore the example you are looking for is absolutely trivial:

public static void main(String[] args) {
   synchronized(String.class) {
       String.class.wait();
   }
}

and this hangs forever. Technically, though, it is not a deadlock, which requires two or more threads involved in a closed cycle where each thread waits for the next one to unblock it.




回答3:


Say thread 1 enters a synchronized block on method A and then waits. Thread 2 then attempts to enter the synchronized block on method A. Thread 1 is waiting for a notify, and thread 2 is waiting on the synchronized block. Everything is now waiting. Some other thread will have to notify the object on which thread 1 is waiting. This is just one scenario that can create a deadlock. There are all kinds of ways to do it.




回答4:


Something close to wait/notify deadlock:

public class Example
{
    volatile boolean isNotified = false;

    public synchronized void method1() {
        try
        {
            isNotified = false;
            while (!isNotified)
                wait();
            notifyAll();
            System.out.println("Method 1");
        } catch (InterruptedException e) {/*NOP*/}
    }

    public synchronized void method2() {
        try {
            isNotified = true;
            while (isNotified)
                wait();
            notifyAll();

            System.out.println("Method 2");
        } catch (InterruptedException e) {/*NOP*/}

    }

    public static void main(String[] args)
    {
        Example example = new Example();

        Thread thread1 = new Thread()
        {

            public void run()
            {
                example.method1();
            }
        };

        Thread thread2 = new Thread()
        {

            public void run()
            {
                example.method2();
            }
        };

        thread1.start();
        thread2.start();
    }
}


来源:https://stackoverflow.com/questions/19908498/deadlocks-using-wait-and-notify

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!