Thread. Join usage) plus personal understanding

Source: Internet
Author: User

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

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.