Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How does join work  RSS feed

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am preparing for SCJP and hence was going through the Threads chapter. I want to know the significance of the join method used in Threads as i am not sure of how the join method works. Can someone illustrate me the significance and how the join method works with a sample code so that i can clear my doubts.

Thanks in advance
Pradeep
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does these tutorials/sample codes help you?

The idea of join is to make a thread wait for some other thread to finish execution.
Sometimes, while doing multi-threaded programming, you would execute some tasks in parallel, but *only* after all threads have finished execution, you need to publish the result.
In such cases, join will come to your rescue.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling join() on a thread (say T1) would keep the current thread waiting till the thread it joins (say T2) dies off.

However, the current thread won't release any locks it has acquired while being in the wait state. So if T2 attempts to enter a method synchronized on some monitor,whose lock has already been acquired by T1,calling T2.join() through T1 would result in a deadlock.

You may want to try this by making the method Resource.m2,in the following code, synchronized.




[ August 26, 2008: Message edited by: Ajay Saxena ]
[ August 26, 2008: Message edited by: Ajay Saxena ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!