溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

淺談C#中多線程ManualResetEvent和AutoResetEvent

發布時間:2020-07-17 09:22:05 來源:億速云 閱讀:744 作者:小豬 欄目:開發技術

小編這次要給大家分享的是淺談C#中多線程ManualResetEvent和AutoResetEvent,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

在多線程開發中,時常用到ManualResetEvent 與AutoResetEvent 。 它們如同道路交通中的信號燈。兩者之間有什么區別呢?

共同點:

均繼承EventWaitHandle 接口,因此,均具有以下功能:
Reset() //紅燈
Set() //綠燈
WaitOne() // 等待信號

不同點:

AutoResetEvent 收到 Set 后 , 一次只能執行一個線程,其它線程繼續 WaitOne 。
ManualResetEvent 收到 Set 后,所有處理 WaitOne 狀態線程均繼續執行。

msdn 提到(如果沒有線程 處于WaitOne() 狀態,而調用 Set ,AutoResetEvent將保持Set 狀態):

調用Set信號AutoResetEvent釋放等待線程。 AutoResetEvent 將保持終止狀態直到一個等待線程釋放,并自動返回到非信號狀態。 如果沒有線程處于等待狀態,狀態將無限期地保持已發出信號。

因此通常WatiOne 之前,先 Reset() 一下,清除Set 信號

需要注意的是(兩個 Set 調用之間時間較短,第二個 Set 信號可能會丟失,因此連續 Set 調用,中間需要 Sleep 一定時間):

不能保證的每個調用Set方法將釋放一個線程。 如果兩次調用太靠近在一起,以便第二次調用前釋放線程發生,只有一個線程被釋放。 就像第二次調用未發生。 此外,如果Set時沒有等待的線程調用和AutoResetEvent已終止,則調用不起作用。

有網友說:

AutoResetEvent.Set() = ManualResetEvent.Set() + ManualResetEvent.Reset();

個人理解 ,這只是原理層面含義,實際使用過程中,有差別的,如下示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testManualResetEvent
{
  class Program
  {
    static object objManualResetEvent = new object();
    static System.Threading.ManualResetEvent manu = new System.Threading.ManualResetEvent(false);
     //static System.Threading.AutoResetEvent manu = new System.Threading.AutoResetEvent(false);
    static void Main(string[] args)
    {
      
      for (int i = 0; i < 10; i++)
      {
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(() => { Product(); }));
        t.Start();
      }
      
      manu.Set();
      manu.Reset();

      Console.ReadKey();
    }

    static void Product()
    {
      manu.WaitOne(10000);
      Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
    }
  }
}

實際執行結果 , 在 執行 set 后 reset 前 ,有多少個線程喚起執行,無法預料:

淺談C#中多線程ManualResetEvent和AutoResetEvent

需要加鎖 ,確保一次通過一個線程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testManualResetEvent
{
  class Program
  {
    static object objManualResetEvent = new object();
    static System.Threading.ManualResetEvent manu = new System.Threading.ManualResetEvent(false);
     //static System.Threading.AutoResetEvent manu = new System.Threading.AutoResetEvent(false);
    static void Main(string[] args)
    {
      
      for (int i = 0; i < 10; i++)
      {
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(() => { Product(); }));
        t.Start();
      }
      
      manu.Set();

      //System.Threading.Thread.Sleep(100); //連續 set 需要 sleep
      //manu.Set();
      //manu.Reset();

      //System.Threading.Thread.Sleep(100);
      //manu.Set();
      //manu.Reset();

      Console.ReadKey();
    }

    static void Product()
    {
      lock (objManualResetEvent)
      {
        manu.WaitOne(10000);          manu.Reset();
        Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
      }
    }
  }
}

執行結果:

淺談C#中多線程ManualResetEvent和AutoResetEvent

看完這篇關于淺談C#中多線程ManualResetEvent和AutoResetEvent的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女