When calling the join method of another thread in the first-line thread, the thread is blocked until the end of the other thread is executed, such as Java code.
- Using system;
- Namespace testthreadjoin
- {
- 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 );
- X. Join ();
- Console. writeline ("this is main. {0}", 2 );
- Console. Readline ();
- }
- Static void F1 ()
- {
- System. Threading. Thread y = new system. Threading. Thread (new system. Threading. threadstart (F2 ));
- Y. Start ();
- Y. Join ();
- Console. writeline ("this is F1. {0}", 1 );
- }
- Static void F2 ()
- {
- Console. writeline ("this is F2. {0}", 1 );
- }
- }
- }
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: annotation // X. Join (); Result: This is main.1 this is main.2 this is f2.1 this is f1.1