4 Replies Latest reply: Oct 21, 2008 2:55 AM by 843785 RSS

    DeadLock issue

    843785
      public class DeadLock {
           static class Friend {
                private final String name;
      
                public Friend(String name) {
                     this.name = name;
                }
      
                public String getName() {
                     return this.name;
                }
      
                public synchronized void bow(Friend bower) {
                     System.out.format("%s: %s has bowed to me!%n", this.name, bower
                               .getName());
      
                     bower.bowBack(this);
      
                }
      
                public synchronized void bowBack(Friend bower) {
      
                     System.out.format("%s: %s has bowed back to me!%n", this.name,
                               bower.getName());
      
                }
           }
      
           public static void main(String[] args) throws Exception {
                final Friend alphonse = new Friend("Alphonse");
                final Friend gaston = new Friend("Gaston");
                Thread t1 = new Thread(new Runnable() {
                     public void run() {
                          alphonse.bow(gaston);
                     }
                });
      
                Thread t2 = new Thread(new Runnable() {
                     public void run() {
                          gaston.bow(alphonse);
                     }
                });
      
                t1.start();
      
                t2.start();
      
           }
      }
      Can anyone explain why this code would result in a deadlock?
        • 1. Re: DeadLock issue
          843785
          A synchronized instance method acquires a monitor on 'this' before it executes. That means both your Friend instances first acquire a monitor on themselves, and then they need to get a monitor on the other class, which is already in use. Both threads wait until the other releases its lock...
          • 2. Re: DeadLock issue
            800378
            according to me it should not result in deadlock.
            • 3. Re: DeadLock issue
              843785
              Ravi_Gupta wrote:
              according to me it should not result in deadlock.
              According to me you are wrong.

              It may not deadlock depending on your luck. But it certainly can deadlock.
              • 4. Re: DeadLock issue
                843785
                Ravi_Gupta wrote:
                according to me it should not result in deadlock.
                You're right. Well written code should not result in deadlocks!

                It will deadlock since 2 different threads try to get a lock on the same monitor. Gaston's thread will first lock Gaston's monitor, and Alphonse's will lock Alphonses monitor. Then Gaston tries to call a synchronized method bowBack on Alphonse, but it cannot execute the method body until it gets a lock on Alphonses monitor. It waits until that lock is released. On the other hand, Alphonse tries to call bowBack on Gaston. This requires getting a lock on Gaston's monitor, which is already locked by Gaston's thread. So neither thread can continue... which is called a deadlock.

                Wether it ends up in a deadlock, depends on timing. If either thread reaches bowBack before the other gets to bow then no deadlock will occur.