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

C# 中如何使用Thread

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

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

Thread 編程

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

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

線程創(chuàng)建好之后,還需要調(diào)用 Start 方法去啟動(dòng),下面的代碼展示了如何去實(shí)現(xiàn),哦,對(duì)了,上面的 MyThreadMethod方法會(huì)在新的線程上被調(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)

一個(gè)創(chuàng)建好的線程,它的生命周期內(nèi)會(huì)有多個(gè)狀態(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)一個(gè) Thread 對(duì)象創(chuàng)建好之后,它的狀態(tài)就是 Unstarted,然而當(dāng) Start 方法啟動(dòng)之后,線程的狀態(tài)將會(huì)從 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());
    }

控制線程的 前臺(tái)和后臺(tái)

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

你可以通過(guò) IsBackground 屬性來(lái)設(shè)置 Thread 的前臺(tái)或者后臺(tái),下面的代碼展示了如何去實(shí)現(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();
    }

除了啟動(dòng)線程,還可以通過(guò) Suspend() 和 Resume() 方法來(lái) 掛起 和 恢復(fù) 線程, 值得注意的是,你只能 恢復(fù) 你之前通過(guò) 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 方法來(lái)實(shí)現(xiàn)多線程之間的同步。

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

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

  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 是先啟動(dòng)的,是不是很好的演示了優(yōu)先級(jí)的概念。

線程是昂貴的,因?yàn)榫€程的整個(gè)生命周期需要消耗太多的資源,比如:初始化,上下文切換,釋放使用的資源 等等,所以在用 多線程 之前需要想好是否真的要這么做,當(dāng)用多線程的時(shí)候,適當(dāng)?shù)氖褂?線程池 (ThreadPool) 是一個(gè)非常好的做法,畢竟線程池內(nèi)部會(huì)幫你自動(dòng)創(chuàng)建,釋放,調(diào)度線程,你只需要傻傻的用即可,同時(shí)也是提升程序響應(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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

日照市| 泾源县| 康乐县| 皋兰县| 淮滨县| 平乐县| 阜阳市| 公安县| 攀枝花市| 博白县| 随州市| 凌海市| 安宁市| 马龙县| 永和县| 德阳市| 无极县| 广安市| 沾益县| 紫金县| 开远市| 望江县| 河间市| 新巴尔虎右旗| 汕尾市| 衡南县| 威宁| 南康市| 马山县| 沾益县| 金塔县| 焉耆| 容城县| 宝鸡市| 上杭县| 枣庄市| 和龙市| 扶余县| 漳平市| 阳东县| 全南县|