How this java code produces deadlock?

前端 未结 5 1711
被撕碎了的回忆
被撕碎了的回忆 2020-12-21 10:23

i am going through oracle docs for deadlock.. i found this code

public class Deadlock {
    static class Friend {
        private final String name;
                 


        
相关标签:
5条回答
  • 2020-12-21 11:04

    in the end of bow A and G call bowBack, leading to a call of G.bow from A and a A.bow from G while bow of A and G is synchronized. So so they both wait for each other to finish.

    0 讨论(0)
  • 2020-12-21 11:07

    You have 2 objects, alphonse and gaston and 2 threads, Thread1 and Thread2

    Suppose this happens:

    1. Thread1: alphonse enters the bow() method. and will hold a lock on the alphonse object

    2. Thread2: gaston enters the bow() method. and will hold a lock on the gaston object

    3. Thread1: alphonse while in the bow() method, calls bowBack() on the gaston object.

      -> Thread1 will block, since Thread2 already has the lock on gaston

    4. Thread2: gaston while in the bow() method, calls bowBack() on the alphonse object.

      -> Thread2 will block, since Thread1 already has the lock on alphonse

    So now Thread1 is waiting for Thread2. And Thread2 is waiting for Thread1. This is a deadlock.

    0 讨论(0)
  • 2020-12-21 11:08

    If you put a Thread.sleep(1000) after printing the first line and before making the call to bowBack, you should see a deadlock. This deadlock can happen anyway, it's will be rare.

    You have two threads and two locks being acquired is different orders. This can leave each thread holding one lock but unable to get the second lock. i.e. a deadlock.

    Note: threads take a lot of time to start which means the first thread can run to completion before the second one starts, so it is unlikely you will see the problem.


    Here is a puzzler for you. This creates a deadlock, can you see why?

    class A {
        static final int i;
        static {
            i = 128;
    
            Thread t = new Thread() {
                public void run() {
                    System.out.println("i=" + i);
                }
            };
            t.start();
            try {
               t.join();
            } catch (InterruptedException e) {
               Thread.currentThread().interrupt();
            }
        }
    
    0 讨论(0)
  • 2020-12-21 11:13

    As you are dealing with multithreading the operations in the two threads may happen in any order with respect to each other. So imagine both actors in this case execute bow and then they both try to execute bow_back. As bow and bow_back are synchronize both objects will be locked and you will not be able to perform bow_back on any of them. Both objects will wait until the other one is "free" and this will not happen as bow will not return before it "bows back".

    0 讨论(0)
  • 2020-12-21 11:14

    The dealock will happen if both enters the bow() method at the same time, or during

    System.out.println();
    

    If you don't see both of the "has bowed back to me!" messages, then the deadlock occured!

    If the first thread terminated before the second started there will be no deadlock.

    Extend the code with Thread.sleep(1000);

    public synchronized void bow(Friend bower) {
    System.out.println(....);
    Thread.sleep(1000);
    ...
    }
    

    then both threads enter bow() and dealock will occur.

    0 讨论(0)
提交回复
热议问题