一级女人毛片人一女人-一级女性大黄生活片免费-一级女性全黄久久生活片-一级女性全黄生活片免费-国产美女在线一区二区三区-国产美女在线观看

始創于2000年 股票代碼:831685
咨詢熱線:0371-60135900 注冊有禮 登錄
  • 掛牌上市企業
  • 60秒人工響應
  • 99.99%連通率
  • 7*24h人工
  • 故障100倍補償
您的位置: 網站首頁 > 幫助中心>文章內容

.NET中線程的生命周期

發布時間:  2012/9/11 17:06:09

讓一個線程進入睡眠狀態

當我們創建一個線程后,我們需要調用線程對象的Start()方法來調度那個線程。在這時,CLR將會為作為構造函數參數傳遞給線程對象的方法地址分配一個時間片。一旦線程開始執行,它就可以在操作系統處理其他線程時回到睡眠狀態或者退出狀態。我們可以使用線程類的Sleep()方法讓一個線程進入睡眠狀態。如果你正在等待一個資源并且你想在稍后繼續嘗試訪問這個資源時,Sleep()方法是很重要的。舉個例子,假設你的程序由于無法訪問需要的資源而導致其不能繼續執行時,你可能想要在幾毫秒之后嘗試繼續訪問資源,在這種情況下讓線程在再次嘗試訪問資源之前睡眠一段時間是一個很好的方式。

Sleep()方法有兩種重載方式。第一種重載方法有一個整型參數,并會按照指定的毫秒時間暫停線程執行。例如,如果你向線程傳遞值100,那么線程將會暫停100毫秒。這個方法將會讓線程進入WaitSleepJoin狀態。讓我們看一個例子,thread_sleep2.cs:

  1. /*************************************  
  2. /* Copyright (c) 2012 Daniel Dong  
  3. *  
  4. * Author:Daniel Dong  
  5. * Blog: www.cnblogs.com/danielWise  
  6. * Email: guofoo@163.com  
  7. *  
  8. */ 
  9. using System;  
  10. using System.Collections.Generic;  
  11. using System.Text;  
  12. using System.Threading;  
  13. namespace SimpleThread  
  14. {  
  15. public class ThreadSleep  
  16. {  
  17. public static Thread worker;  
  18. public static Thread worker2;  
  19. public static void Main()  
  20. {  
  21. Console.WriteLine("Entering the void Main!");  
  22. worker = new Thread(new ThreadStart(Counter));  
  23. worker2 = new Thread(new ThreadStart(Counter2));  
  24. //Make the worker2 Object as highest priority  
  25. worker2.Priority = ThreadPriority.Highest;  
  26. worker.Start();  
  27. worker2.Start();  
  28. Console.WriteLine("Exiting the void Main!");  
  29. Console.ReadLine();  
  30. }  
  31.  public static void Counter()  
  32. {  
  33. Console.WriteLine("Entering Counter");  
  34. for (int i = 1; i <50; i++)  
  35. {  
  36. Console.Write(i + " ");  
  37. if (i == 10)  
  38. {  
  39. Console.WriteLine();  
  40. Thread.Sleep(1000);  
  41. }  
  42. }  
  43. Console.WriteLine("Exiting Counter");  
  44. }  
  45. public static void Counter2()  
  46. {  
  47. Console.WriteLine("Entering Counter2");  
  48. for (int i = 51; i <100; i++)  
  49. {  
  50. Console.Write(i + " ");  
  51. if (i == 70)  
  52. {  
  53. Console.WriteLine();  
  54. Thread.Sleep(5000);  
  55.  }  
  56. }  
  57. Console.WriteLine("Exiting Counter2");  
  58. }  
  59. }  

 

Counter()方法從1到50計數,當到達10以后睡眠1000毫秒。Counter2()方法從51~100計數,當到達70時睡眠5000毫秒。下面是輸出結果:

注:以上是在多核CPU下運行的結果,單核CPU 執行情況可能與上圖有所出入。

第二種重載方法有一個TimeSpan類型參數,當前線程會按照TimeSpan的值暫停一段時間。TimeSpan是System命名空間中的一個類。TimeSpan有一些很有用的屬性并會返回基于時鐘時間間隔。

我們可以使用FromSeconds()和FromMinutes()來確定睡眠時間。下面是一個例子,thread_sleep3.cs:

 

  1. public static void Counter()  
  2. {  
  3. Console.WriteLine("Entering Counter");  
  4. for (int i = 1; i <50; i++)  
  5. {  
  6. Console.Write(i + " ");  
  7. if (i == 10)  
  8. {  
  9. Console.WriteLine();  
  10. Thread.Sleep(TimeSpan.FromSeconds(1));  
  11.  }  
  12. }  
  13. Console.WriteLine("Exiting Counter");  
  14. }  
  15. public static void Counter2()  
  16. {  
  17. Console.WriteLine("Entering Counter2");  
  18. for (int i = 51; i <100; i++)  
  19. {  
  20.  Console.Write(i + " ");  
  21. if (i == 70)  
  22. {  
  23. Console.WriteLine();  
  24. Thread.Sleep(TimeSpan.FromSeconds(5));  
  25. }  
  26. }  
  27. Console.WriteLine("Exiting Counter2");  

 

輸出結果與thread_sleep2類似。

中斷一個線程

當讓一個線程睡眠時,它實際會進入WaitSleepJoin狀態。如果線程處理睡眠狀態,那么在它超時退出之前唯一可以喚醒線程的方式是使用Interrupt()方法。Interrupt()方法將讓線程回到調度隊列中去。讓我們看一個例子,thread_interrupt.cs:

 

  1. /*************************************  
  2. /* Copyright (c) 2012 Daniel Dong  
  3. *  
  4. * Author:Daniel Dong  
  5. * Blog: www.cnblogs.com/danielWise  
  6. * Email: guofoo@163.com  
  7. *  
  8. */ 
  9. using System;  
  10. using System.Collections.Generic;  
  11. using System.Text;  
  12. using System.Threading;  
  13.  namespace SimpleThread  
  14. {  
  15. public class Interrupt  
  16. {  
  17. public static Thread sleeper;  
  18. public static Thread worker;  
  19. public static void Main()  
  20. {  
  21. Console.WriteLine("Entering the void Main!");  
  22. sleeper = new Thread(new ThreadStart(SleepingThread));  
  23. worker = new Thread(new ThreadStart(AwakeThread));  
  24. sleeper.Start();  
  25. worker.Start();  
  26. Console.WriteLine("Exiting the void Main!");  
  27. Console.ReadLine();  
  28. }  
  29.  public static void SleepingThread()  
  30. {  
  31. for (int i = 1; i <50; i++)  
  32. {  
  33. Console.Write(i + " ");  
  34. if (i == 10 || i == 20 || i == 30)  
  35. {  
  36. Console.WriteLine("Going to sleep at: " + i);  
  37. try 
  38. {  
  39. Thread.Sleep(20);  
  40. }  
  41. catch (ThreadInterruptedException ex)  
  42. {  
  43. Console.WriteLine(ex.Message);  
  44. }  
  45.  }  
  46. }  
  47. }  
  48. public static void AwakeThread()  
  49. {  
  50. for (int i = 51; i <100; i++)  
  51. {  
  52. Console.Write(i + " ");  
  53. if (sleeper.ThreadState == ThreadState.WaitSleepJoin)  
  54. {  
  55. Console.WriteLine("Interrupting the sleeping thread.");  
  56. sleeper.Interrupt();  
  57. }  
  58. }  
  59. }  
  60. }  

 

在上面的例子中,當計數器的值為10, 20 和 30 時第一個線程會睡眠。第二個線程會檢查第一個線程是否已經進入睡眠狀態。如果是的話,它將中斷第一個線程并使它回到調度隊列中去。Interrupt()方法是讓睡眠線程重新醒來的最好方式,當線程等待的資源可用且你想讓線程繼續運行時你可以使用這個方法。輸出結果與下面顯示的類似:


本文出自:億恩科技【www.laynepeng.cn】

服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經營性ICP/ISP證:贛B2-20080012
  • 服務器/云主機 24小時售后服務電話:0371-60135900
  • 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
  • 專注服務器托管17年
    掃掃關注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
      1
     
     
     
     

    0371-60135900
    7*24小時客服服務熱線