File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explain Join() method of Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explain Join() method of Thread" Watch "Explain Join() method of Thread" New topic

Explain Join() method of Thread

Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
Join() is non static method of Thread class per my understanding what it does is, it let one thread join at to the end of another Thread.
Lets see there are three Thread t1, t2,and t3. I want to say lets start t1 then t3 then t2. Is this right please explain the what I need to do bases on the senerio and my code below

Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = new Thread();



Thanks, AR
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
When you call start on some thread, it will go to runnable state, then based on thread scheduler it may go to running state.

Your code is saying:

Start 3 threads, order in which they start executing run is not guaranteed and then wait until thread 1 is finished, thread 3 is finished, and thread 2 is finished, then proceed (with some code below).

If you want to be sure, that thread t1 enters run method as first, then you need to synchronize this action and wait (in your main thread) with starting other threads.
Eega Sudheer

Joined: Jun 12, 2007
Posts: 3
Here join() causes the mainthread to wait until child threads execution completes.
I agree. Here's the link:
subject: Explain Join() method of Thread
It's not a secret anymore!