File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

join()

 
Amanda Fu
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone can explain to me what does Thread.join() do? The explanation in JDK is too simple. Thanks!
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It waits for the thread that you're joining to finish.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amanda Fu
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm cleared now. Thanks Corey, your example is very helpful. And thanks Ron too.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amanda
Try this link.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic