Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How does thread.join functions  RSS feed

 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pasted below in the code for join method in Thread class.



It checks it the current thread is alive. If the thread is alive it then calls wait on the thread. In this case it seems that if t.join() was called it will be t that is made to wait. Whereas it should be the thread from which t.join() was called, should wait. Secondly what happens to the lock when any of the thread goes waiting?
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not know clearly...me also interested to know the answer of this question.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anupam Sinha wrote:
It checks it the current thread is alive. If the thread is alive it then calls wait on the thread. In this case it seems that if t.join() was called it will be t that is made to wait. Whereas it should be the thread from which t.join() was called, should wait. Secondly what happens to the lock when any of the thread goes waiting?



Please take a look at the JavaDoc for the wait() method -- you'll notice that it is the calling threads that waits.

Henry
 
Subhasis Gorai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Anupam Sinha wrote:
It checks it the current thread is alive. If the thread is alive it then calls wait on the thread. In this case it seems that if t.join() was called it will be t that is made to wait. Whereas it should be the thread from which t.join() was called, should wait. Secondly what happens to the lock when any of the thread goes waiting?



Please take a look at the JavaDoc for the wait() method -- you'll notice that it is the calling threads that waits.

Henry


But who notifies the waiting thread(the calling thread), I mean is there any hook which gets executed as soon as the joining thread finishes its execution(job execution) and calls the notify()/notifyAll() on the joining thread? Or is it the combination of "Spurious wake up" and while(isAlive()) that works? Please clarify.
Thanks,
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhasis Gorai wrote:
But who notifies the waiting thread(the calling thread), I mean is there any hook which gets executed as soon as the joining thread finishes its execution(job execution) and calls the notify()/notifyAll() on the joining thread? Or is it the combination of "Spurious wake up" and while(isAlive()) that works? Please clarify.
Thanks,


When a thread completes (returns or exceptions from run() method), it goes into code that cleans up after the thread. One of the operations is a notifyAll() method call on the newly completed thread's Thread object.

This is also why you should not use a thread's Thread object for notification, as you will be sharing the notification object.

Henry
 
Subhasis Gorai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. If I am not wrong Thread.exit() is probably the hook who does such clean up job, I didn't find out any code for notifying over there.
Can you please point out where exactly it's happening?

Thanks again!
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhasis Gorai wrote:Thanks Henry. If I am not wrong Thread.exit() is probably the hook who does such clean up job, I didn't find out any code for notifying over there.
Can you please point out where exactly it's happening?


Not sure what you are talking about. There is no such exit() method. And it is not a hook. It is part of the thread cleanup code. It has been a while since I looked at that part of the JVM, so don't have more detail than that.

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!