• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

How does thread.join functions

 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not know clearly...me also interested to know the answer of this question.
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23931
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic