Thread的使用
class Program { List<ManualResetEvent> manualEvents = new List<ManualResetEvent>(); static void Main(string[] args) { Stopwatch watch = new Stopwatch(); watch.Start(); Console.WriteLine("-----------------Thread多线程 --------------------------"); Console.WriteLine("-----------------开始 主线程id为:{0} --------------------------", Thread.CurrentThread.ManagedThreadId); List<Thread> threadList = new List<Thread>(); Thread myThread = new Thread(() => { Console.WriteLine("我是子线程1"); Thread.Sleep(2000); }); //myThread.IsBackground = true; //设置为后台线程,主程序关闭所有线程均关闭 myThread.Start(); Thread myThread1 = new Thread(() => { Console.WriteLine("我是子线程2"); Thread.Sleep(2000); }); //myThread1.IsBackground = true; //设置为后台线程,主程序关闭所有线程均关闭 myThread1.Start(); threadList.Add(myThread); //利用join方法进行线程等待 foreach (Thread thread in threadList) { thread.Join(); } watch.Stop(); Console.WriteLine("----------------- 结束 主线程id为:{0} 总耗时:{1}--------------------------", Thread.CurrentThread.ManagedThreadId, watch.ElapsedMilliseconds); Console.ReadKey(); } }
原文:https://www.cnblogs.com/chenze-Index/p/11593136.html