aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "join()" Watch "join()" New topic
Author

join()

Amanda Fu
Ranch Hand

Joined: Jul 17, 2003
Posts: 31
Does anyone can explain to me what does Thread.join() do? The explanation in JDK is too simple. Thanks!
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It waits for the thread that you're joining to finish.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
It makes a thread stop and wait for the termination of another thread. Let's take a simple example...
We have two threads, thread A and thread B. Thread A starts first, which handles a GUI for an application. At some point, the application needs to process a file. Rather than having the GUI freeze up and do nothing but process the file, thread A spawns a new thread, B, to process the file so that thread A is still able to handle user events.
Now, let's say that, while thread B is running, the user selects an option to view the results of the file processing. Obviously, it wouldn't be good for thread A to go on until thread B is done processing the file. Therefore, in the code for thread A, you could do this:

That will cause thread A to stop until thread B is done with it's work (its run method completes).
I know that's a rather contrived example, but I hope it helps.
Corey


SCJP Tipline, etc.
Amanda Fu
Ranch Hand

Joined: Jul 17, 2003
Posts: 31
Hi I'm cleared now. Thanks Corey, your example is very helpful. And thanks Ron too.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Amanda
Try this link.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: join()