• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Question on Threads

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am basically creating two threads and giving both the threads, the same job. Now in the run method, I am checking the thread name and joining it to another thread. But I am getting errors. Can someone tell me why? Also, since I am using a synchronized block in the run method and since join keeps the lock, I believe even if I succeed with the compilation process, it will cause a deadlock. Because say if thread t1 entered the run method and I do, t2.join, then t1 is waiting for t2 to complete but has the lock and t2 is waiting for the lock. Am I correct? I am providing the code.



1). Why won't the above code compile?
2). Am I correct that if the code would have compiled, it would lead to a deadlock?
3). How can I call join on t1 from t2 or join on t2 from t1 without causing a deadlock in the above situation?

Thanks.
[ September 15, 2008: Message edited by: Arjun Reddy ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code won't compile because Thread1 and Thread2 in your run() method have not been declared. The following code will compile (and that's about all it's good for):



As you noted, once one of the Threads attempts to join with the other, you will wait forever. You don't actually even need the synchronized block to create the deadlock condition--if you remove it, then each Thread will get to the join statement and then wait forever for the other Thread to complete.

I'm not clear what the goal of your code is once it actually does something, but join() is typically used when you want a Thread to complete before something else happens. So you would put the join outside the run method:



If you want two run() methods to be cooperating (for instance a Thread that gets user input and a Thread that processes it), you should use wait() and notify() to allow one to wait on the other while both are still active.
 
I found some pretty shells, some sea glass and this lovely tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic