Usage of thread. Join ()
// Blocks the calling thread until a thread terminates
When the join method of another thread is called in the first thread, the thread is blocked until the end of the other thread.
For example
Class Program
{
Static void main ()
{
System. Threading. Thread x = new system. Threading. Thread (new system. Threading. threadstart (F1 ));
X. Start ();
Console. writeline ("this is main. {0}", 1); // The first
X. Join (); // The main thread waits until x ends.
Console. writeline ("this is main. {0}", 2); // fourth
Console. Readline ();
}
Static void F1 ()
{
System. Threading. Thread y = new system. Threading. Thread (new system. Threading. threadstart (F2 ));
Y. Start ();
Y. Join (); // The X-ray waits for the end of thread y, and the second
Console. writeline ("this is F1. {0}", 1 );
}
Static void F2 ()
{
Console. writeline ("this is F2. {0}", 1); // y thread execution, third
}
}
There are three threads processing (including the main thread). You can check the execution result.
Result:
This is main.1
This is f2.1
This is f1.1
This is main.2
If: Comment // X. Join ();
Result:
This is main.1
This is main.2
This is f2.1
This is f1.1
In addition, you can take a look at the differences between the following methods. If you are interested, you can use the following example to test
Thread. Suspend (): suspends a thread, or does not work if the thread has been suspended;
Thread. Resume (): continue the suspended thread;
Thread. Interrupt (): Stop a thread in the wait, sleep, or join thread state;
Thread. Sleep (): the specified number of milliseconds that will block the current thread;
Name: Plain
Mail: xzp91032@163.com