一个线程在执行的过程中,可能调用另一个线程,前者可以称为调用线程,后者成为被调用线程。
Thread.Join方法的使用场景:调用线程挂起,等待被调用线程执行完毕后,继续执行。
如下案列:
当NewThread调用Join方法的时候,MainThread就被停止执行,直到NewThread线程执行完毕,才会接着再去执行。(转载)
static void Main(string[] args) { Thread.CurrentThread.Name = "MainThread"; Thread newThread = new Thread(new ThreadStart(Program.ThreadFuncOne)); newThread.Name = "NewThread"; for (int j = 0; j < 20; j++) { if (j == 10) { newThread.Start(); newThread.Join(); } else { Console.WriteLine(Thread.CurrentThread.Name + " j = " + j); } } Console.Read(); } private static void ThreadFuncOne() { for (int i = 0; i < 10; i++) { Console.WriteLine(Thread.CurrentThread.Name + " i = " + i); } Console.WriteLine(Thread.CurrentThread.Name + " has finished"); }