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

Usage of Join

Harish Narayana

Joined: Mar 29, 2010
Posts: 3
When do we use Join in Multithreading

Rahul Kumar Tiwari

Joined: Jun 09, 2008
Posts: 14
Harish Narayana wrote:When do we use Join in Multithreading

Suppose There is a two thread A and B. and if you started both thread by A.start(), and B.Start(). After that you see that both thread start and work according to the thread scheduler. But during this run if you want that B thread will complete their execution before A thread then write code B.join(). This method join the thread B to to the execution pool of thread A and complete their execution first. and after that A will run.
Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3782

And, welcome to JavaRanch

This is an example for what Rahul said. There are three overloaded versions of this method. You can find them in the documentation of java.lang.Thread class.

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Harish Narayana

Joined: Mar 29, 2010
Posts: 3
Hi Rahul,
It will be very helpful if I get a real time example.
sood rahul

Joined: Jun 30, 2010
Posts: 3
We use join() whenever we need to attach a thread at the end of another thread.

For eg. Suppose there are two threads : t1 and current thread(main thread) and we invoke start method on t1 and write t1.join();
This will join the current thread(main thread) at the end of thread t1.
As soon as this statement is executed main thread waits for thread t1 to finish.

Rahul Sood
Harish Narayana

Joined: Mar 29, 2010
Posts: 3
Thanks a ton Devaka Cooray
I agree. Here's the link:
subject: Usage of Join
It's not a secret anymore!