• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Usage of Join

 
Harish Narayana
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When do we use Join in Multithreading
 
Rahul Kumar Tiwari
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harish Narayana
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,
Thanks.
It will be very helpful if I get a real time example.
 
sood rahul
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harish Narayana
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton Devaka Cooray
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic