Thread join method, Threadjoin Method
// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng
/**
* Join
Public final void join ()
Throws InterruptedException waits for the thread to terminate.
Throw:
InterruptedException-if any thread breaks the current thread. When this exception is thrown, the interruption status of the current thread is cleared.
In the following example, after A calls the join method, the process is allocated only when the thread where A is not running.
**/
// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng
Public class joinThread {
Public static void main (String [] args) throws Exception {
ThreadTest5 t = new ThreadTest5 ();
Thread A = new Thread (t );
Thread B = new Thread (t );
A. start ();
A. join (); // here A calls the join method of Thread. The main function assigns the Thread to A. After A finishes running, it releases the Thread. To other objects.
B. start ();
For (int I = 1; I <20; I ++)
{
System. out. println ("apple on the tree" + I );
}
System. out. println ("Apple is gone ");
}
}
// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng
Class ThreadTest5 implements Runnable
{
Public void run ()
{
For (int I = 1; I <10; I ++)
{
System. out. println (Thread. currentThread (). getName () + "eat apple" + (I ));
}
}
}
// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng
/**
* The running result is
* Thread-0 eat apple 1
Thread-0 eat apple 2
Thread-0 eat apple 3
Thread-0 eat apple 4
Thread-0 eat apple 5
Thread-0 eat apple 6
Thread-0 apple 7
Thread-0 eat apple 8
Thread-0 eat apple 9
Apple 1 in the tree
Apple 2 falling from the tree
Apple 3 in the tree
Apple 4 in the tree
Apple 5 falling from the tree
Apple 6 falling from the tree
Thread-1 eat apple 1
Apple 7 falling from the tree
Thread-1 eat apple 2
Apple 8 falling from the tree
Thread-1 eat apple 3
Apple 9 in the tree
Thread-1 eat apple 4
Apple 10 in the tree
Thread-1 eat apple 5
Apple 11 falling from the tree
Thread-1 eat apple 6
Thread-1 eat apple 7
Thread-1 eat apple 8
Thread-1 eat apple 9
Apple 12 falling from the tree
Apple 13 in the tree
Apple 14 falling from the tree
Apple 15 in the tree
Apple 16 in the tree
Apple 17 on the tree
Apple 18 falling from the tree
Apple 19 on the tree
Apple is gone
Thread-0 is the Thread where A is located. When the Thread where A is located is finished running, the later Thread is competed by the main function and the process B.
*/
// Copyrightliupengcheng
// Http://www.cnblogs.com/liupengcheng
What about join of Thread ??
Public class T {
Public static void main (String [] args ){
Final Thread A = new Thread (new Runnable (){
Public void run (){
For (int I = 1; I <= 10; I ++ ){
Try {
Thread. sleep (500 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println ("Thread A has slept" + I * 0.5 + "seconds ");
}
}
});
Final Thread B = new Thread (new Runnable (){
Public void run (){
For (int I = 1; I <= 10; I ++ ){
Try {
Thread. sleep (300 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println ("Thread B has slept" + I * 0.3 + "seconds ");
}
}
});
Thread C = new Thread (new Runnable (){
Public void run (){
Try {
A. join ();
B. join ();
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println ("Thread A and B has been over !! Now its C's turn! ");
}
});
A. start ();
B. start ();
C. start ();
}
}
ThreadcurrentThread () join ()
When you asked this question, I still don't know what join is. I thought it was similar to windows api WaitForSingleObject (hThread, INFINITE ).
Inspired by your question, your question may be the answer to your question. The main thread waits for the end of the main thread, but the main itself is not over, so this forms a circle, the main. sleep (unlimited) features...