• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Threads Join method

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
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

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: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
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

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: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks @Henry for clearing this out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic