Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Concurrency - Why is a deadlock happening in this example?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Concurrency - Why is a deadlock happening in this example?

    I took this example from here.

    Both bow() and bowBack() are synchronized methods. Let's call the first thread defined on Line 28 (which calls alphonse.bow(gaston);) as A, and the other thread defined on Line 31 as B.

    Even if we assume that both the threads invoke the method bow() at the same time, they are invoking it for different objects. If it was a single object, and thread B tried to invoke bow() while it was already invoked by thread A, thread B would block (suspend temporarily) until bow() invoked by thread A returned. But in this case, none of the threads will block because they are invoking the method on different objects.

    Then in the method body of bow(), the last statement is bower.bowBack(this);. Again, since bowBack() is a synchronized method, if bower represented the same object in both the method calls (that is the call in thread A and the one in thread B), there was a chance for the threads to block since bowBack() would appear to being executed by the other thread.

    But here, bower represents different objects for both the method calls (calls to bowBack() in the thread A and thread B).

    So why do the threads block forever, waiting for each other forever?

    Java Code:
    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) {
            final Friend alphonse =
                new Friend("Alphonse");
            final Friend gaston =
                new Friend("Gaston");
            new Thread(new Runnable() {
                public void run() { alphonse.bow(gaston); }
            }).start();
            new Thread(new Runnable() {
                public void run() { gaston.bow(alphonse); }
            }).start();
        }
    }
    When Deadlock runs, it's extremely likely that both threads will block when they attempt to invoke bowBack. Neither block will ever end, because each thread is waiting for the other to exit bow.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Concurrency - Why is a deadlock happening in this example?

    Both bow() and bowBack() are synchronised.
    So code that calls bowBack() on an object that is already locked due to something executing bow() will block.

    In this case, bow() calls bowBack() on the other Friend object, and the odds are that that object is locked as the bow() method on it will have been called.
    And that object can't release its lock as it's trying to call bowBack() on the first one...which is blocked.
    So deadlock.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Concurrency - Why is a deadlock happening in this example?

    The deadlock occurs on bowBack().

    Alphonse has entered synchronized bow(), which creates a lock on the instance Alphonse (because the whole method is synchronized). So does Gaston on instance Gaston. When Alphonse is trying to access synchronized bowBack() it will try to lock on the instance Gaston because the entrie method is synchronized. This is not possible since Gaston has the lock. Same goes for Gaston when he tried to call alphonse.bowBack(), Alphonse has that lock. Neither friends can give it up since they are waiting for the other friend to give it up.

    Java Code:
    Found one Java-level deadlock:
    =============================
    "Thread-1":
      waiting to lock monitor 0x00007fbd000036c8 (object 0x000000071988f320, a forums.Deadlock$Friend),
      which is held by "Thread-0"
    "Thread-0":
      waiting to lock monitor 0x00007fbd00006218 (object 0x000000071988f368, a forums.Deadlock$Friend),
      which is held by "Thread-1"
    
    Java stack information for the threads listed above:
    ===================================================
    "Thread-1":
    	at forums.Deadlock$Friend.bowBack(Deadlock.java:40)
    	- waiting to lock <0x000000071988f320> (a forums.Deadlock$Friend)
    	at forums.Deadlock$Friend.bow(Deadlock.java:32)
    	- locked <0x000000071988f368> (a forums.Deadlock$Friend)
    	at forums.Deadlock$2.run(Deadlock.java:17)
    	at java.lang.Thread.run(Thread.java:745)
    "Thread-0":
    	at forums.Deadlock$Friend.bowBack(Deadlock.java:40)
    	- waiting to lock <0x000000071988f368> (a forums.Deadlock$Friend)
    	at forums.Deadlock$Friend.bow(Deadlock.java:32)
    	- locked <0x000000071988f320> (a forums.Deadlock$Friend)
    	at forums.Deadlock$1.run(Deadlock.java:11)
    	at java.lang.Thread.run(Thread.java:745)
    
    Found 1 deadlock.

    Edit: the line numbers are a bit off since I reformatted the code
    gimbal2 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Concurrency - Why is a deadlock happening in this example?

    @SurfMan @Tolls Thank you so very much.

Similar Threads

  1. Replies: 0
    Last Post: 10-15-2014, 01:44 PM
  2. What is happening here?
    By nakkim in forum New To Java
    Replies: 4
    Last Post: 02-12-2014, 08:25 AM
  3. NumberFormatException- shouldn't be happening?
    By RoKr93 in forum New To Java
    Replies: 5
    Last Post: 07-04-2013, 02:17 AM
  4. I don't know what is happening
    By Fred1 in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 12:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •