• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt about deadlock using join and synchronized

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Oscar Calderon
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic