*
The moose likes Threads and Synchronization and the fly likes regarding join() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "regarding join()" Watch "regarding join()" New topic
Author

regarding join()

veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67

thread on which join() is invoked should finish the task and then the calling thread will continue its execution.



But in the above code have have called join() in Mythread so calling thread main() have to finish the execution ,yes it has finished the task of main() thread but child thread task is not finished, it will stop.

Question:
1. Is there any way that join() should be used in certain threads or pattern only?

Please clarify it, thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4180
    
  21

veena bijur wrote:But in the above code have have called join() in Mythread so calling thread main() have to finish the execution ,yes it has finished the task of main() thread but child thread task is not finished, it will stop.

You call join() on the MyThread instance, inside the MyThread instance - which means the thread blocks until it (itself) is finished (or interrupted). This can never work: MyThread will wait until MyThread is finished, but MyThread can't finish because it is waiting. So your application will most likely hang forever.

Question:
1. Is there any way that join() should be used in certain threads or pattern only?
Is there a way it should be used? Yes, you call it on a different thread when you want to wait for that thread to finish. You never call it on the current thread, because then it can never return.


Steve
veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67

Thanks a lot, now i am clear with the concept.

Thanks again.
 
 
subject: regarding join()