last day (14 days later) » 

7:10 AM
1
A: Understanding why deadlock happens in this implementation

Maroun MarounHere's a possible scenario for the first case: Thread 1 acquires Lock1 and goes to sleep for 10 milliseconds. Now Thread 2 acquires Lock2 and goes to sleep for 10 milliseconds. Now Thread 1 tries to acquire Lock2, but can't get it since it's acquired by Thread 2, while Thread 2 tries to get Lo...

 
Thanks. I can now understand the first case. But the second case is still a bit hazy. So thread 2 is waiting for first lock. As soon as it is released by thread 1, what happens then?
 
@AttitudeMonger Thread 2 gets it and begin the execution of the first statement under the synchronized block.
 
Which is System.out.println("Thread 2: Holding lock 1 & 2...");? And it releases lock 2, which is acquired by thread 1, and it prints System.out.println("Thread 1: Holding lock 1 & 2..."); ?
 
@AttitudeMonger Exactly.
 
Why does second thread release lock 2 after acquiring lock 1?
 
7:10 AM
@AttitudeMonger Because it exits the first synchronized block.
 
Meaning? The second synchronized block is inside the first one, right?
 
Actually I didn't notice the nested synchronized blocks you have there
is there a reason why you have them nested?
 
No I haven't done anything. This is some sample code I got online here: tutorialspoint.com/java/java_thread_deadlock.htm
 
Then it's even easier
while the first thread gets the first lock
it goes to sleep
not the second thread gets the second lock
both are waiting for each other's lock
and they cannot proceed
now switching the order gives you this:
 
No no, I got the first case..why it is happening.
I am trying to understand why it isn't the same in the second case..
 
7:16 AM
the first thread executes the first statement and goes to sleep
it has Lock1
now the second thread 2 tries to execute the method, but it can't
it waits for the first lock. Not thread 1 wakes up
and gets Lock 2
it still have the first lock!
it finishes the second statement
and release both locks
now the JVM is free to decide which thread to run. It can pick the first or the second
 
Aha, on finishing a synchronized block, it automatically releases the lock..
 
So basically I will run it down in layman's term from my perspective.. Please correct me if I am wrong.. And sorry for making you spoonfeed me.. :)
In case 2:
1. Thread 1 acquires lock1, so thread 2 can't start even if it wants to, because lock1 has been acquired by thread 1. So it waits.

2. Thread 1, after acquiring lock1, goes to sleep for 10 ms, and after this, it acquires lock2 as well. So at this time, it has acquired both lock1 and lock2. Finally it executes the statement inside lock2 conditional, and exits, releasing both lock1 and lock2.

3. Then JVM can decide which thread to run next..
4. It my be thread 2, it may be thread 1 again..
In the later case, thread 2 continues to wait even more..
But because at some point, depending on what JVM does, it may become free, this is not a deadlock.
But in the first case, because each is waiting for a resource locked by the other, they will never get free. Right?
 
Exactly!
 
Ah. Gimme some time to see if it has correctly entered my fat brains..
 
7:24 AM
you can even answer your own question :) good explanation.
I tried to make my answer clearer
good luck!
 
He he.. so one thing, in the second example, the second thread may also run first, right? Instead of the first one?
The logic will be the same, of course..
 
sure
it's up to JVM
 
Cool. Thanks!
 

last day (14 days later) »