• Post Reply Bookmark Topic Watch Topic
  • New Topic

Will it make any difference if Thread.join is made before starting the Threads.  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

What is the correct way to call join Method on a Thread or will it make any difference if Thread.join is made before starting the Threads.


Assume that there are two Threads(Both extending Thread class) One and Two .


One one = new One();// Consider One is a Thread that implements Runnable
Two two = new Two();// Consider Two is a Thread that implements Runnable

Thread one = new Thread(one);
Thread two = new Thread(two);

1st Approach :
one.start();
two.start();

two.join(); // Join call is made after starting the Threads.

=========

2nd Approach :

two.join();// Join call is made before starting the Threads.


one.start();
two.start();

Please share your knowledge .Thanks
 
Sandeep Sanaboyina
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
join() is used to wait till the thread dies. If you give join before start, since the thread is not started yet, it will immediately come out of join().

So what is the purpose of using join in the 2nd approach

Why don't you try running different versions and figure it out for yourself.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks .
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
start() method will always start a new thread and when that thread get initialized it will call a run method
and what join() method wil do is it wil wait for thread to die.
So if the thread is not even started then calling join on that thread has no use.
It will simply jump to next statement

Happy Learning
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation . Thanks .
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!