Thread join method, Threadjoin Method

Source: Internet
Author: User

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...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.