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