• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads Join method  RSS feed

 
Brendon McCullum
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am presently studying Thread's join method. As per the docs:



There are 2 other constructors as well, allowing thread to specify the time to wait, before it can die.
What's the difference between the timeout specified in join and sleep method? In both cases, the thread waits for it to get over and I guess in both cases, the waiting thread can interrupt the blocking thread to get inside the method.

As an example, consider following code:


If I specify the timeout as 40000 in either sleep or join method, thread2 has to wait, before it can print out the String.
Moreover, I don't understand the point where docs say, that join lets other threads to wait
until the blocking thread's timeout expires, or it dies. Can you let me know any scenario or example, where the join's timeout period is very high, but the thread actually finishes its work before that, and dies before the timeout actually expires. If not, I guess this is pretty much the same as sleep.
Thanks.
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Having a thread join() itself will serve absolutely no purpose -- as there is no way that a thread will finish what it is doing, as it is busy waiting for itself to finish what it is doing. The thread join() method is generally used for a thread to what for another thread to finish.

Henry
 
Brendon McCullum
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry, what I've found out is that join affects the thread it's called on (that executing thread stops executing unless the thread on which join is called completes or time-outs after a certain interval). Is this the sole use of join? This is to say that join stops the currently executing thread, and joins it to the end of the thread on which it was called.
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brendon McCullum wrote:@Henry, what I've found out is that join affects the thread it's called on (that executing thread stops executing unless the thread on which join is called completes or time-outs after a certain interval). Is this the sole use of join? This is to say that join stops the currently executing thread, and joins it to the end of the thread on which it was called.


This is the description of the join() method (that takes no arguments) from the Java Documentation...

join()

Waits for this thread to die.


Short and sweet. And the one that takes a timeout is only a few more words -- but it pretty much matches what you described.

Henry
 
Brendon McCullum
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @Henry for clearing this out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!