C# Thread.Join 方法阻塞调用线程,直到某个线程终止时为止
我们可以这么理解:当NewThread调用Join方法的时候,MainThread就被停止执行,
直到NewThread线程执行完毕。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace ConsoleApplication1
- {
- class TestThread
- {
- 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");
- }
- static void Main(string[] args)
- {
- Thread.CurrentThread.Name = "MainThread";
- Thread newThread = new Thread(new ThreadStart(TestThread.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();
- }
- }
- }