aspose file tools
The moose likes Threads and Synchronization and the fly likes Query on Thread join Method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Query on Thread join Method" Watch "Query on Thread join Method" New topic
Author

Query on Thread join Method

Ram Venkata
Greenhorn

Joined: Apr 10, 2007
Posts: 8
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

Joined: Apr 10, 2007
Posts: 8
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Ram Venkata
Greenhorn

Joined: Apr 10, 2007
Posts: 8
Thanks a lot . Appreciate the explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query on Thread join Method
 
Similar Threads
Join method
Q. Methods of a dead thread.
Data exchange between threads
Threads - join()
notify()