1.什麼是執行緒?
程序作為作業系統執行程式的基本單位,擁有應用程式的資源,程序包含執行緒,程序的資源被執行緒共享,執行緒不擁有資源。
2.前臺執行緒和後臺執行緒的區別?
程式關閉時,後臺執行緒直接關閉,但前臺執行緒會執行完後關閉。
透過Thread類新建執行緒預設為前臺執行緒。其他方式建立的都是後臺執行緒。
多執行緒的幾種方式
一. 非同步多執行緒
static void Main(string[] args) { Console.WriteLine("----------主程式開始,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); for(int i = 0; i< 5;i++) { Action<string> action = t => { for(int k = 0; k< 1000000000;k++) { } Console.WriteLine("當前引數是{0},當前執行緒是{1}",t,Thread.CurrentThread.ManagedThreadId); }; action.BeginInvoke("引數是" + i, null, null); } Console.WriteLine("----------主程式結束,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); } }
二. Threads執行緒
static void Main(string[] args) { Console.WriteLine("----------主程式開始,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); for(int i = 0; i< 5;i++) { ParameterizedThreadStart threadStart = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前引數是{0},當前執行緒是{1}", t, Thread.CurrentThread.ManagedThreadId); }; Thread thread = new Thread(threadStart); thread.Start(i); } Console.WriteLine("----------主程式結束,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
三. ThreadPool執行緒池
static void Main(string[] args) { Console.WriteLine("----------主程式開始,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); for(int i = 0; i< 5;i++) { WaitCallback waitCallback = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前引數是{0},當前執行緒是{1}", t, Thread.CurrentThread.ManagedThreadId); }; ThreadPool.QueueUserWorkItem(waitCallback, i); } Console.WriteLine("----------主程式結束,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
四. Task
static void Main(string[] args) { Console.WriteLine("----------主程式開始,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); TaskFactory taskFactory = new TaskFactory(); for (int i = 0; i< 5;i++) { Action<object> action = t => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前引數是{0},當前執行緒是{1}", t, Thread.CurrentThread.ManagedThreadId); }; taskFactory.StartNew(action,i); } Console.WriteLine("----------主程式結束,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
五. Parallel
static void Main(string[] args) { Console.WriteLine("----------主程式開始,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action action1 = () => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前引數是1,當前執行緒是{0}", Thread.CurrentThread.ManagedThreadId); }; Action action2 = () => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前引數是2,當前執行緒是{0}", Thread.CurrentThread.ManagedThreadId); }; Action action3 = () => { for (int k = 0; k < 1000000000; k++) { } Console.WriteLine("當前引數是3,當前執行緒是{0}", Thread.CurrentThread.ManagedThreadId); }; Parallel.Invoke(action1, action2, action3); Console.WriteLine("----------主程式結束,執行緒ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read(); }
上面簡單介紹了5種多執行緒的使用方式,後續文章會記錄每種多執行緒的詳細使用方法.
轉載:
https://www.cnblogs.com/yangxi1081/p/9639958.html