c#多線程thread實例詳解
阿新 • • 發佈:2018-09-16
++ {0} 詳解 實現 需要 i++ tostring 返回 主程序
1. Thread線程啟動
由於ThreadStart是一個委托,所以可以簡化寫法
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); for(int i = 0;i< 5;i++) { ThreadStart method = () => TestThread("無參數"); Thread thread = new Thread(method); thread.Start(); } Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); } static void TestThread(string name) { Console.WriteLine("TestThread Start name:{0} 當前線程id:{1} 當前時間:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString()); long sum = 0; for (int i = 0; i < 10000000; i++) { sum += i; } Console.WriteLine("TestThread End name:{0} 當前線程id:{1} 當前時間:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString()); }
2. Thread線程傳遞參數
上面的例子沒專遞沒有參數的,現在開始傳遞參數
public delegate void ParameterizedThreadStart(object obj);
這個是Thread可以傳遞參數的方法委托,使用這個委托,可以傳遞參數。
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); for(int i = 0;i< 5;i++) { ParameterizedThreadStart method = o => TestThread(o.ToString()); Thread thread = new Thread(method); thread.Start("param" + i); } Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); } static void TestThread(string name) { Console.WriteLine("TestThread Start name:{0} 當前線程id:{1} 當前時間:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString()); long sum = 0; for (int i = 0; i < 10000000; i++) { sum += i; } Console.WriteLine("TestThread End name:{0} 當前線程id:{1} 當前時間:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString()); }
3. Thread線程等待
有時候,主線程需要等待子線程執行完後在執行,Thread的線程等待我們使用join()來實現
static void Main(string[] args) { Console.WriteLine("----------主程序開始,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); List<Thread> list = new List<Thread>(); for(int i = 0;i< 5;i++) { ParameterizedThreadStart method = o => TestThread(o.ToString()); Thread thread = new Thread(method); thread.Start("param" + i); list.Add(thread); } foreach (var t in list) { t.Join(); } Console.WriteLine("----------主程序結束,線程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); } static void TestThread(string name) { Console.WriteLine("TestThread Start name:{0} 當前線程id:{1} 當前時間:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString()); long sum = 0; for (int i = 0; i < 10000000; i++) { sum += i; } Console.WriteLine("TestThread End name:{0} 當前線程id:{1} 當前時間:{2}", name, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString()); }
4. Thread線程獲取返回值
Thread沒發獲取返回值,請註意
5. Thread線程回調
Thread沒有回調,我們自定義一個回調
private void ThreadCallBack(ThreadStart method, Action callback) { ThreadStart threadStart = () => { method.Invoke(); callback.Invoke(); }; Thread thread = new Thread(threadStart); thread.Start(); }
c#多線程thread實例詳解