java - Where is the Deadlock? -


this tutorial java docs deadlock. don't threads blocked. since synchronized thought 1 thread 'll enter bow. both entered bow. [one waits [but when?]]

then issue?

when add comments [print statements trace]. there's no deadlock. how?

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 me!%n", this.name, bower.getname());             bower.bowback(this);         }         public synchronized void bowback(friend bower) {             system.out.format("%s: %s"                 + " has bowed 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() {             @override             public void run() {                 // system.out.println("thread 1");                 alphonse.bow(gaston);                 // system.out.println("th: gaston bowed alphonse");             }         }).start();          new thread(new runnable() {             @override             public void run() {                //  system.out.println("thread 2");                 gaston.bow(alphonse);               //  system.out.println("2.gaston waiting alph bowed");             }         }).start();     } } 

the output is:

alphonse: gaston  has bowed me! gaston: alphonse  has bowed me! 

none bows back!

there 2 important things understand here: 1) each concurrently running thread doing? , 2) locks involved?

last thing first, created 2 instances of friend class: alphonse , gaston. each of objects possesses own lock. there 2 locks: alphonse's lock , gaston's lock. when enter synchronized method of object, locks object's lock. lock released (unlocked) when synchronized method returns.

now threads. first thread, let's call alphone's thread (note capital distinguish thread object, alphonse) this: ("a:" indicates alphonse's thread.)

a: alphonse.bow(gaston) - acquires alphonse's lock a: gaston.bowback(alphonse) - acquires gaston's lock a: both methods return, releasing both locks 

concurrently, gaston's thread this: ("g:" indicates gaston's thread.)

g: gaston.bow(alphonse) - acquires gaston's lock g: alphonse.bowback(gaston) - acquires alphonse's lock g: both methods return, releasing both locks 

now can put these 2 bits of information arrive @ our answer. threads may interleave (i.e., events occur) in different orders. example, deadlock occurs if events occur in order:

a: alphonse.bow(gaston) - acquires alphonse's lock g: gaston.bow(alphonse) - acquires gaston's lock g: attempts call alphonse.bowback(gaston), blocks waiting on alphonse's lock a: attempts call gaston.bowback(alphonse), blocks waiting on gaston's lock 

in case each thread blocked, waiting acquire lock other thread holds. however, neither thread release lock holds because must complete method in order so, cannot since blocked waiting on other thread. stuck in place - deadlocked.

however, possible interleaving (order of events) 1 in 1 of threads completes before other 1 gets going, so:

a: alphonse.bow(gaston) - acquires alphonse's lock a: gaston.bowback(alphonse) - acquires gaston's lock a: both methods return, releasing both locks g: gaston.bow(alphonse) - acquires gaston's lock g: alphonse.bowback(gaston) - acquires alphonse's lock g: both methods return, releasing both locks 

in case there no deadlock. when added println's , got no deadlock, what's happening delay of additional println's affects scheduling and/or rate @ threads run, kind of interleaving instead of 1 deadlocks.

when outcome dependent on ordering of concurrent events (i.e., scheduling or rate @ run) called "race condition". example, in program, thread prints output first depends in order in they're scheduled or rate @ run. whether or not threads deadlock depends on this. perturbing rate (e.g., adding instructions), may affect outcome of race - doesn't mean race has gone away.

not race conditions have potential cause deadlock. however, in experience, deadlocks occur in race conditions. (perhaps knows comment on whether that's case, or commonly case?)

deadlocks not easy understand @ first; did best explain, please let me know if have follow-up questions on post. :)


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -