Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Query on Thread join Method  RSS feed

 
Ram Venkata
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package test;
public class JoinSample{
public static void main(String[] args) {
System.out.println("Main started,");
Thread theThread = new AAA();
Thread theBThread=new BBB();
try {
theThread.start();
theBThread.start();
if(theBThread.isAlive())
{
System.out.println("Thread B Alive and I am in");
theBThread.join();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("main ready.");
}
}
class AAA extends Thread {
public void run(){
try{
System.out.println("---------Thread AAA--------------Started");
for (int counter=0; counter<10; counter++){
sleep(500);
System.out.print(counter);
}
}catch(Exception e){}
finally{System.out.println("AAA Done");}
}}

class BBB extends Thread{
public void run(){
try{
System.out.println("---------Thread BBB-------------Started");
for (int counter=0; counter<10; counter++){
sleep(500);
System.out.print(counter);
}
}catch(Exception e){}
finally{System.out.println("BBB Done");}
}
}

------------------------------------------------------------------------------
[Output]
Main started,
Thread B Alive and I am in
---------Thread AAA--------------Started
---------Thread BBB-------------Started
0011223344556677889AAA Done
9BBB Done
main ready.

Query-> Isn't Thread B supposed to be completed first?
Please explain.

Regards,
Ram
 
Ram Venkata
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going thru the above URL

http://www.cs.mtu.edu/~shene/NSF-3/e-Book/FUNDAMENTALS/thread-management.html


a join method always makes the parent thread(Main thread) wait for the child threads to finish rather than the concurrent threads?

Is that a fair understanding?

regards,
Ram
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
join() makes the thread that calls it wait until the other thread finishes. If you just start a thread and then join it, there's no chance for anything to happen in parallel. If you start a thread, do some long running stuff in your own thread and then join the other thread, all that long running stuff can happen "at the same time" as the other thread. It's handy when you want to run two or many things and maybe combine the results but you don't know which will finish first.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

to be precise,
justAnotherThread.join();

makes the current thread wait until justAnotherThread is dead.

In Ram Venkata's example, the main thread is the one who waits, as the call for join() is in the main method. You can see this also because the last line of output is "main ready".
theBThread is not influenced by the call of join()!


Hint: insert
System.out.println(Thread.currentThread().getName());
just before your call to join



Yours,
Bu.
 
Ram Venkata
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot . Appreciate the explanation
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!