Thread series -- thread. Join ()

Source: Internet
Author: User

 

The thread. Join () method, as its name implies, adds something to a thread.

On msdn, the function is to block the "Call thread" until a thread ends.

The translation explanation is a bit obscure. For example:

Static void main ()

{

Thread t = new thread (New threadstart (threadmethod ));

T. Start ();

T. Join ();

Console. writeline ("I am main thread ");

Console. Read ();

}

Void threadmethod ()

{

...

}

From aboveCode, We can see there are two threads: main thread and thread t

Return to join. The caller here is the main thread. The main thread calls the join method of thread t, which causes the main thread to be blocked until the execution of thread t is complete and is returned to the main thread.

Simply understand, call T in the main thread. join (), that is, the code of the T thread added to the main thread. After the T thread is executed, the main thread (caller) can run normally.

 

Sample Code:

 

Code

    Static     Void  Main (  String  [] ARGs)
{
// Autoresetevent autoevent = new autoresetevent (false );
Thread regularread = New Thread ( New Threadstart (threadmethod ));
Regularread. Start ();
Threadpool. queueuserworkitem ( New Waitcallback (workmethod1), regularread );

Console. writeline ( " I am main thread! " );
// Regularread. Join ();
Console. writeline ( " I am main thread! " );
// Autoevent. waitone ();
Console. Read ();
}
Static Void Threadmethod ()
{
For ( Int I = 0 ; I < 5 ; I ++ )

{
Console. writeline ( " Threadone executes for {0} Times, executing threadmethod, is {1} from the thread pool. " , I + 1 , Thread. currentthread. isthreadpoolthread ? "" : " Not " );
Thread. Sleep ( 1000 );
}


}
Static Void Workmethod1 ( Object Stateinfo)
{
Console. writeline ( " Threadtwo, executing workmethod, is {0} form the thread pool. " , Thread. currentthread. isthreadpoolthread ? "" : " Not " );
// (Autoresetevent) stateinfo). Set ();
(Thread) stateinfo). Join ();
For ( Int I = 0 ; I < 5 ; I ++ )
{
Console. writeline ( " Threadtwo execution at {0}, executing workmethod, is {1} form the thread pool. " , I + 1 , Thread. currentthread. isthreadpoolthread ? "" : " Not " );
}

}

 

 

There are three threads in the Code: the main thread, regularread, and one thread in the thread pool. The regularread. Join () method is called in the thread pool;

 

Execution result:

 

We can see that the content in the thread loop in the thread pool is executed only after the regularread content is executed.

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.