This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about deadlock using join and synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about deadlock using join and synchronized" Watch "Doubt about deadlock using join and synchronized" New topic
Author

Doubt about deadlock using join and synchronized

Oscar Calderon
Ranch Hand

Joined: Nov 27, 2009
Posts: 32
Hi to all, actually i'm studying for SCJP for April 10 :S . Iwas reading about Threads because i haven't understand yet all concerning Threads. I was coding a java class that works with 3 threads, as you can see in the code:



As you can see, i have 3 girlfriend threads and i want to first to run "Estefany" thread, second "Helen" thread and finally "Ada" thread, so in the run method when the thread is "Ada" then i call join over "Helen" thread to forcing "Ada" thread to run after "Helen" thread dies and after that when the thread is "Helen", join to force "Helen" thread run after "Estefani" thread dies.

So, because i was studying also the synchronized keyword because i don't understand very well that i leave my run method as synchronized and using join. I know that if i use join to handle the 3 threads there will no problem because the for loop will execute first for Estefany, second for Helen and third for Ada, but i think that the difference in my class between using the joins and using synchronized is that only with synchronized i can't guarantee the order of threads execution but each one will run the foor loop individually, and with joins i can guarantee the order of threads execution and also the for loop execution individually, but, when i test my source code it looks like a deadlock, but that only when first thread to run is Ada or Helen, because Estefani pass the if's of the run method without enter in one of them. But for example, if Ada thread runs first, it enters in the first if and in the join call the program freezes, i think that it's a deadlock but i don't understand why.

Thanks in advance


Wanna grow? Learn, live, love people and enjoy life
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Oscar Calderon wrote:... but, when i test my source code it looks like a deadlock, but that only when first thread to run is Ada or Helen, because Estefani pass the if's of the run method without enter in one of them. But for example, if Ada thread runs first, it enters in the first if and in the join call the program freezes, i think that it's a deadlock but i don't understand why.


If Ada enters the synchronized run() method first, then Helen and Estefani needs to wait til Ada releases the lock before they can enter the synchronized run() method.

Now... Ada needs for Helen to finish before she will continue. But Helen needs Ada to release the lock before she can enter the run() method. Ada is waiting for Helen. Helen is waiting for Ada.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Oscar Calderon
Ranch Hand

Joined: Nov 27, 2009
Posts: 32
Ok, thanks for your answer, but now i have a doubt. Modifying a little bit my code, i understand that the next things are the same:



But, when i run the code with that modification, it runs and there's no deadlock, the thing that i don't understand is why it runs well if is almost the same?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Oscar Calderon wrote:
But, when i run the code with that modification, it runs and there's no deadlock, the thing that i don't understand is why it runs well if is almost the same?


That's because it is not the same. Ada is waiting for Helen without holding the synchronization lock. Ada is waiting for Helen. Helen is *not* waiting for Ada.

Henry
Oscar Calderon
Ranch Hand

Joined: Nov 27, 2009
Posts: 32
Ohh, i think that i understand you now for example, with the second modification (using synchronized(this) , sorry if i'm wrong ) imagine this: Ada call run() method first, then enters to first if and it joins, waits until Helen ends, now lets say that Estefani call run(), and because Ada couldn't reach the synchronized block because it is after join() call in the first if, Estefani pass the if's and enters to synchronized(this) block and loop, while that if JVM gives to Helen opportunity to run, Helen call run() and enters to second if and call join() to wait until Estefani dies (sounds weird XD ) and Estefani runs again, finishes it's loop, release the lock over "this" and die, Helen now get chance to run and enters the synchronized(this), finishes it's loop, release the lock and die so Ada gets a chance to run and so on.

But with public synchronized void run() i think that for example, first Ada call run(), lock it and call join() to wait to Helen until dies, after that Helen or Estefani tries to call run() but they can't because Ada gets the lock and is waiting for Helen.

Thanks for your answers, it helped me a loot, the next thing that i don't understand is when you use synchronized(object) over object that is for example, a class member, but i'm going to read more about it.

Thanks
 
jQuery in Action, 2nd edition
 
subject: Doubt about deadlock using join and synchronized
 
Similar Threads
deadlock
Question on Threads
Chapter 9: syncronized(this)
Did I Really Find A Bug In Synchronization Or Am I Confused?
Interaction between threads