最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 中如何使用Thread

 更新時間:2021年01月28日 09:04:41   作者:一線碼農(nóng)  
這篇文章主要介紹了C# 中使用 Thread的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

線程是進(jìn)程中的最小執(zhí)行單元,多線程是指在給定時間內(nèi)擁有多個線程的能力,并且可以調(diào)度它們從而在某一時刻處理多個操作,微軟的 .Net Framework 提供了 Thread 來幫助我們完成多線程開發(fā)。

Thread 編程

要想使用 Thread,需要在程序中引用 System.Threading 命名空間,然后再提供一個供線程調(diào)度的方法,這個方法是通過 Thread 中的 ThreadStart 委托代理的,下面的代碼展示了如何創(chuàng)建線程。

Thread t = new Thread(new ThreadStart(MyThreadMethod));

線程創(chuàng)建好之后,還需要調(diào)用 Start 方法去啟動,下面的代碼展示了如何去實現(xiàn),哦,對了,上面的 MyThreadMethod方法會在新的線程上被調(diào)度,而不是調(diào)用線程。

  class Program
  {
    static void Main(string[] args)
    {
      Thread t = new Thread(new ThreadStart(MyThreadMethod));
      t.Start();
      Console.Read();
    }
    static void MyThreadMethod()
    {
      Console.WriteLine("Hello World!");
    }
  }

展示線程狀態(tài)

一個創(chuàng)建好的線程,它的生命周期內(nèi)會有多個狀態(tài),比如:Aborted, Background, Running, Stopped, Suspended, Unstarted 等等,這些狀態(tài)在 Thread 中是用 ThreadState 枚舉表示的,如下代碼所示:

  [Flags]
  public enum ThreadState
  {
    Running = 0,
    StopRequested = 1,
    SuspendRequested = 2,
    Background = 4,
    Unstarted = 8,
    Stopped = 16,
    WaitSleepJoin = 32,
    Suspended = 64,
    AbortRequested = 128,
    Aborted = 256
  }

當(dāng)一個 Thread 對象創(chuàng)建好之后,它的狀態(tài)就是 Unstarted,然而當(dāng) Start 方法啟動之后,線程的狀態(tài)將會從 Unstarted 切換到 Running 狀態(tài),下面的代碼展示了這種輪轉(zhuǎn)。

    static void Main(string[] args)
    {
      Thread t = new Thread(new ThreadStart(MyThreadMethod));
      Console.WriteLine("The thread's state is:" + t.ThreadState.ToString());
      t.Start();
      Console.WriteLine("The thread's state is:" + t.ThreadState.ToString());
    }

控制線程的 前臺和后臺

一個線程要么是前臺線程要么是后臺線程,如果你是通過顯式的方式創(chuàng)建線程,它便是前臺線程,前后線程最大的區(qū)別在于:應(yīng)用程序退出的前提必須是程序內(nèi)的所有前臺線程都得到退出,相反,應(yīng)用程序的退出不依賴于后臺線程。

你可以通過 IsBackground 屬性來設(shè)置 Thread 的前臺或者后臺,下面的代碼展示了如何去實現(xiàn)。

    static void Main(string[] args)
    {
      Thread t = new Thread(new ThreadStart(MyThreadMethod));
      t.Start();
      t.IsBackground = true;
      Console.WriteLine(“The thread's background status is: “+t.IsBackground.ToString());
      Console.Read();
    }

除了啟動線程,還可以通過 Suspend() 和 Resume() 方法來 掛起 和 恢復(fù) 線程, 值得注意的是,你只能 恢復(fù) 你之前通過 Suspend 方法 掛起的線程,如下代碼所示:

    static void Main(string[] args)
    {
      Thread t = new Thread(new ThreadStart(MyThreadMethod));
      t.Start();
      t.Suspend(); //Suspends the newly created thread
      t.Resume(); //Resumes the suspended thread
      Console.Read();
    }

值得注意的是,現(xiàn)在的 Thread.Suspend() Thread.Resume() 方法都是被標(biāo)記成棄用的狀態(tài)了,取而代之的做法是:使用 AutoResetEventEventWaitHandle 方法來實現(xiàn)多線程之間的同步。

設(shè)置線程優(yōu)先級

可以給一個線程賦予優(yōu)先級,從而和內(nèi)存中的其他線程爭搶 CPU 時間,在 C# 中是使用 ThreadPriority 枚舉來表示,大體上有如下值: Lowest, BelowNormal, Normal, AboveNormal 和 Highest,下面的代碼展示了如何給這兩個線程賦予優(yōu)先級。

  class Program
  {
    static void Main(string[] args)
    {
      Thread thread1 = new Thread(new ThreadStart(Method1));
      Thread thread2 = new Thread(new ThreadStart(Method2));

      thread1.Priority = ThreadPriority.Highest;
      thread2.Priority = ThreadPriority.Lowest;

      thread2.Start();
      thread1.Start();

      Console.Read();
    }
    static void Method1()
    {
      for (int i = 0; i < 10; i++)
      {
        Console.WriteLine("First thread: " + i);
      }
    }
    static void Method2()
    {
      for (int i = 0; i < 10; i++)
      {
        Console.WriteLine("Second thread: " + i);
      }
    }
  }

從上面的輸出結(jié)果中可以看出,Thread1 先于 Thread2 執(zhí)行完,即使 Thread2.Start 是先啟動的,是不是很好的演示了優(yōu)先級的概念。

線程是昂貴的,因為線程的整個生命周期需要消耗太多的資源,比如:初始化,上下文切換,釋放使用的資源 等等,所以在用 多線程 之前需要想好是否真的要這么做,當(dāng)用多線程的時候,適當(dāng)?shù)氖褂?線程池 (ThreadPool) 是一個非常好的做法,畢竟線程池內(nèi)部會幫你自動創(chuàng)建,釋放,調(diào)度線程,你只需要傻傻的用即可,同時也是提升程序響應(yīng)的利器。

譯文鏈接:https://www.infoworld.com/article/3035134/how-to-work-with-threads-in-c.html

以上就是C# 中如何使用Thread的詳細(xì)內(nèi)容,更多關(guān)于c# 使用 Thread的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

琼结县| 福海县| 平果县| 泾源县| 师宗县| 潼关县| 和田县| 桑植县| 黄骅市| 丹寨县| 长寿区| 莱州市| 台东县| 吴旗县| 北安市| 谷城县| 江津市| 静乐县| 沈丘县| 贵定县| 东乡族自治县| 察雅县| 惠州市| 临潭县| 凤阳县| 深水埗区| 沾益县| 墨竹工卡县| 贺兰县| 保定市| 高台县| 合江县| 龙州县| 峡江县| 莒南县| 山阳县| 鄄城县| 邢台县| 马鞍山市| 津市市| 广汉市|