• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Threads join() method

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

I was reading about join() from the tutorial's provided by sun

http://java.sun.com/docs/books/tutorial/essential/concurrency/join.html

It says that join() stops the current thread to stop and join it at the end of other thread and it must wait, till that thread completes
and then the current thread will start resuming its work. But i am confused by this line, what does it mean

"The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,
t.join();"

Does it mean t is the currently running thread?? Please help.


 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Zaidi wrote:"The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing, t.join();"
Does it mean t is the currently running thread??



"t" is a currently running Thread object but the code you call the "t.join()" is the one going to wait till Thread "t" completes.
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But doesn't the join() method, cause the current thread of execution to stop till the other has completed.

if t is the currently running thread, shouldn't it mean that t should stop execution and let other thread complete the execution first?
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Zaidi wrote:if t is the currently running thread, shouldn't it mean that t should stop execution and let other thread complete the execution first?



No that means join the Thread "t" so the code calling "t.join()" (which is run by another thread) waits till Thread t completes.
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps this is lot clearer

java.lang.Thread.join()

Waits for this thread to die.

Throws:
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.



The currently executing thread(one which is executing the t.join() instruction) will wait for the 't' instance to complete.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call t.join(), the current thread will stop and wait until thread t has finished executing. When t has finished, the current thread will resume executing.

So it's really simple: if you call t.join(), you make the current thread wait until thread t has finished.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic