I ran the following code from the java
tutorial on Concurrency. This code's objective is to implement 'deadlock'.
I have added some debug messages for a better understanding the flow during runtime.
I have added a static field 'status' and I just update it to check the synchronized access.
In my understanding so far when a Synchronized method is invoked by a thread
,no other thread can invoke the same
method until the first thread that has the lock over the method returns the lock.
But I get the following output: (it actually results in deadlock)
Thread[Thread-0,5,main] setting the status to 1
Thread[Thread-1,5,main] setting the status to 2
Alphonse: GastonThread[Thread-0,5,main] has bowed to me!
Gaston: AlphonseThread[Thread-1,5,main] has bowed to me!
From the output I see that Thread-1 is able to set the status before Thread-0 completes!
How does synchronization work ?