進程(process)和線程(thread)是操做系統的基本概念,可是它們比較抽象,不容易掌握,最近,我讀到一篇材料,發現了一個很好的類比,能夠把它們解釋的清晰易懂。爲接下來學習多線程編程作準備html
計算機的核心是CPU,它承擔了全部的計算任務。它就像一座工廠,時刻在運行。假定工廠的電力有限,一次只能供給一個車間使用。也就是說,一個車間開工的時候,其餘的車間必須停工。背後的含義就是,單個CPU一次只能運行一個任務git
一個 exe 運行一次就會產生一個進程,一個進程裏至少有一個線程:主線程。咱們平時寫的控制檯程序默認就是單線程的,代碼從上往下執行,一行執行完了再執行下一行github
1.沒有進行參數傳遞:編程
static void Main(string[] args) { int i = 5; Thread thread = new Thread(() => { Console.WriteLine("i="+i); //輸出i=6,這裏也有可能i=5,就是當i=6執行以前就執行了這段代碼 }); thread.Start(); i = 6; Console.ReadKey(); }
2.參數傳遞:網絡
static void Main(string[] args) { int i = 5; Thread thread = new Thread((obj) => { Console.WriteLine("i=" + obj); //輸出i=5,這裏取的就是 thread.Start(i);i=5的值 }); thread.Start(i); i = 6; Console.ReadKey(); }
線程默認是「非後臺線程」,一個程序必須全部「非後臺線程」執行結束後程序纔會退出。多線程
把線程設置爲「後臺線程」後,全部「非後臺線程」執行結束後程序就會退出,不會等後臺線程」執行結束ide
thread.IsBackground = true; //設置爲後臺線程post
Thread.Sleep(1000) //讓當前線程睡眠多長時間學習
1.線程的優先級:this
thread.Priority = ThreadPriority.Normal; //能夠設置5個優先級,優先級高的被執行的次數會多的點,平時就設置一個Normal就能夠了
2.線程的終止:
thread.Abort() //終止這個線程
解析:
會在當前執行的代碼上「無風起浪」的拋出 ThreadAbortException。來終止當前線程的執行,在程序中,通常不須要捕獲處理這個異常
3.喚醒線程:
static void Main(string[] args) { Thread thread1 = new Thread(() => { try { Thread.Sleep(5000); } catch (ThreadInterruptedException) { Console.WriteLine("喚醒了線程"); } }); thread1.Start(); Console.ReadKey(); }
thread1.Interrupt(); //喚醒線程就會執行ThreadInterruptedException中catch的方法裏面的喚醒方法
Sleep 是靜態方法,只能是本身主動要求睡,別人不能命令他睡
線程同步:就是解決多個線程同時操做一個資源的問題
thread1.Join(); //表明等待thread1線程執行完畢
示例代碼:
1 class Program 2 { 3 private static int count = 0; 4 static void Main(string[] args) 5 { 6 Thread thread1 = new Thread(() => { 7 for (int i = 0; i < 1000; i++) 8 { 9 count++; 10 Thread.Sleep(1); 11 } 12 13 }); 14 Thread thread2 = new Thread(() => { 15 for (int i = 0; i < 1000; i++) 16 { 17 count++; 18 Thread.Sleep(1); 19 } 20 }); 21 thread1.Start(); 22 thread2.Start(); 23 thread1.Join(); 24 thread2.Join(); 25 Console.WriteLine(count); 26 Console.ReadKey(); 27 } 28 }
解決多個線程同時操做一個資源:用lock加鎖,鎖定一個資源。同時只能有一個線程進入 lock 的對象的範圍,其餘 lock 的線程就要等待
方法一:
1 class Program 2 { 3 private static int count = 0; 4 private static object locker = new object(); 5 static void Main(string[] args) 6 { 7 Thread thread1 = new Thread(() => { 8 for (int i = 0; i < 1000; i++) 9 { 10 lock (locker) 11 { 12 count++; 13 } 14 Thread.Sleep(1); 15 } 16 17 }); 18 Thread thread2 = new Thread(() => { 19 for (int i = 0; i < 1000; i++) 20 { 21 lock (locker) 22 { 23 count++; 24 } 25 Thread.Sleep(1); 26 } 27 }); 28 thread1.Start(); 29 thread2.Start(); 30 thread1.Join(); 31 thread2.Join(); 32 Console.WriteLine(count); 33 Console.ReadKey(); 34 } 35 }
方法二:
方法上標註 [MethodImpl(MethodImplOptions.Synchronized)],這樣一個方法只能同時被一個線程訪問
方法三:
lock 關鍵字就是對 Monitor 的簡化調用,lock 最終就編譯成 Monitor
static void QuQian(string name) { Monitor.Enter(locker);//等待沒有人鎖定 locker 對象,我就鎖定它,而後繼續執行 try { Console.WriteLine(name + "查看一下餘額" + money); int yue = money - 1; Console.WriteLine(name + "取錢"); money = yue;//故意這樣寫,寫成 money--其實就沒問題 Console.WriteLine(name + "取完了,剩" + money); } finally { Monitor.Exit(locker);//釋放 locker 對象的鎖 } }
Monitor.TryEnter(locker) //TryEnter 方法,若是 Enter 的時候有人在佔用鎖,它不會等待,而是會返回false
使用 WebClient 獲取一個網頁而後顯示到 WinForm 中,界面會卡。由於網絡操做阻塞了主線程.對於比較耗時的操做,放到子線程中
private void button1_Click(object sender, EventArgs e) { ThreadPool.QueueUserWorkItem(state=>{ WebClient wc = new WebClient(); string html= wc.DownloadString("https://github.com/"); //TextBox.CheckForIllegalCrossThreadCalls = false; 不要使用這個 this.BeginInvoke(new Action(()=>{ textBox1.Text = html; })); }); }