詳細解析C#多線程同步事件及等待句柄
最近搗鼓了一下多線程的同步問題,發(fā)現(xiàn)其實C#關于多線程同步事件處理還是很靈活,這里主要寫一下,自己測試的一些代碼,涉及到了AutoResetEvent 和 ManualResetEvent,當然還有也簡要提了一下System.Threading.WaitHandle.WaitOne 、System.Threading.WaitHandle.WaitAny和System.Threading.WaitHandle.WaitAll ,下面我們一最初學者的角度來看,多線程之間的同步。
假設有這樣的一個場景,主線程開了一個子線程,讓子線程等著,等主線程完成了某件事情時再通知子線程去往下執(zhí)行,這里關鍵就在于這個怎讓子線程等著,主線程怎通知子線程,一般情況下我們不難想到用一個公共變量,于是咱們就有了下面的代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Class1
{
static bool flag = true;
static void DoWork()
{
Console.WriteLine(" worker thread started, now waiting on event...");
while (flag)
{
}
Console.WriteLine(" worker thread reactivated, now exiting...");
}
static void Main()
{
Console.WriteLine("main thread starting worker thread...");
Thread t = new Thread(DoWork);
t.Start();
Console.WriteLine("main thrad sleeping for 1 second...");
Thread.Sleep(1000);
Console.WriteLine("main thread signaling worker thread...");
flag = false;
}
}
}
雖然目的達到了,但是看著這代碼就糾結,下面該是我們的主角上場了,AutoResetEvent 和 ManualResetEvent,關于這兩者我們暫且認為是差不多了,稍后我會介紹他們的不同,這里以AutoResetEvent為例,其實很多官方的說法太過于抽象,這里通俗地講,可以認為AutoResetEvent就是一個公共的變量(盡管它是一個事件),創(chuàng)建的時候可以設置為false,然后在要等待的線程使用它的WaitOne方法,那么線程就一直會處于等待狀態(tài),只有這個AutoResetEvent被別的線程使用了Set方法,也就是要發(fā)通知的線程使用了它的Set方法,那么等待的線程就會往下執(zhí)行了,Set就是發(fā)信號,WaitOne是等待信號,只有發(fā)了信號,等待的才會執(zhí)行。如果不發(fā)的話,WaitOne后面的程序就永遠不會執(zhí)行。好下面看用AutoResetEvent改造上面的程序:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Class2
{
static AutoResetEvent mEvent=new AutoResetEvent(false);
//static ManualResetEvent mEvent = new ManualResetEvent(false);
static void DoWork()
{
Console.WriteLine(" worker thread started, now waiting on event...");
mEvent.WaitOne();
Console.WriteLine(" worker thread reactivated, now exiting...");
}
static void Main()
{
Console.WriteLine("main thread starting worker thread...");
Thread t = new Thread(DoWork);
t.Start();
Console.WriteLine("main thrad sleeping for 1 second...");
Thread.Sleep(1000);
Console.WriteLine("main thread signaling worker thread...");
mEvent.Set();
}
}
}
這時代碼是不是清爽多了,這里其實你還會看到,把上面的AutoResetEvent換成ManualResetEvent也是沒有問題的,那么它兩之間的區(qū)別是什么呢?個人認為它們最大的區(qū)別在于,無論何時,只要 AutoResetEvent 激活線程,它的狀態(tài)將自動從終止變?yōu)榉墙K止。相反,ManualResetEvent 允許它的終止狀態(tài)激活任意多個線程,只有當它的 Reset 方法被調(diào)用時才還原到非終止狀態(tài)。開下面的代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Class3
{
static AutoResetEvent mEvent = new AutoResetEvent(false);
//static ManualResetEvent mEvent = new ManualResetEvent(false);
static void DoWork()
{
Console.WriteLine(" worker thread started, now waiting on event...");
for (int i = 0; i < 3; i++)
{
mEvent.WaitOne();
//mEvent.Reset();
Console.WriteLine(" worker thread reactivated, now exiting...");
}
}
static void Main()
{
Console.WriteLine("main thread starting worker thread...");
Thread t = new Thread(DoWork);
t.Start();
for (int i = 0; i < 3; i++)
{
Thread.Sleep(1000);
Console.WriteLine("main thread signaling worker thread...");
mEvent.Set();
}
}
}
}
如果你想僅僅把AutoResetEvent換成ManualResetEvent的話,你發(fā)現(xiàn)輸出就會亂套了,為什么呢?
假如有autoevent.WaitOne()和manualevent.WaitOne(),當線程得到信號后都得以繼續(xù)執(zhí)行。差別就在調(diào)用后,autoevent.WaitOne()每次只允許一個線程進入,當某個線程得到信號(也就是有其他線程調(diào)用了autoevent.Set()方法后)后,autoevent會自動又將信號置為不發(fā)送狀態(tài),則其他調(diào)用WaitOne的線程只有繼續(xù)等待,也就是說,autoevent一次只喚醒一個線程。而manualevent則可以喚醒多個線程,當某個線程調(diào)用了set方法后,其他調(diào)用waitone的線程獲得信號得以繼續(xù)執(zhí)行,而manualevent不會自動將信號置為不發(fā)送,也就是說,除非手工調(diào)用了manualevent.Reset()方法,否則manualevent將一直保持有信號狀態(tài),manualevent也就可以同時喚醒多個線程繼續(xù)執(zhí)行。
在上面代碼中,如果將AutoResetEvent換成ManualResetEvent的話,只要要在waitone后面做下reset,就會達到同樣的效果。
之后咱們再來個簡單的例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Class4
{
public static AutoResetEvent mEvent = new AutoResetEvent(false);
public static void trmain()
{
Thread tr = Thread.CurrentThread;
Console.WriteLine("thread: waiting for an event");
mEvent.WaitOne();
Console.WriteLine("thread: got an event");
for (int x = 0; x < 10; x++)
{
Thread.Sleep(1000);
Console.WriteLine(tr.Name + ": " + x);
}
}
static void Main(string[] args)
{
Thread thrd1 = new Thread(new ThreadStart(trmain));
thrd1.Name = "thread1";
thrd1.Start();
for (int x = 0; x < 10; x++)
{
Thread.Sleep(900);
Console.WriteLine("Main:" + x);
if (5 == x) mEvent.Set();
}
while (thrd1.IsAlive)
{
Thread.Sleep(1000);
Console.WriteLine("Main: waiting for thread to stop");
}
}
}
}
是不是更有感覺了?之后咱來看看另外幾個東東:
System.Threading.WaitHandle.WaitOne 使線程一直等待,直到單個事件變?yōu)榻K止狀態(tài);
System.Threading.WaitHandle.WaitAny 阻止線程,直到一個或多個指示的事件變?yōu)榻K止狀態(tài);
System.Threading.WaitHandle.WaitAll 阻止線程,直到所有指示的事件都變?yōu)榻K止狀態(tài)。
然后再來個例子,以WaitAll使用為例:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class other
{
static void Main(string[] args)
{
Random randomGenerator = new Random();
AutoResetEvent[] resets=new AutoResetEvent[5];
for (int i = 0; i < 5; i++)
{
resets[i] = new AutoResetEvent(false);
int wTime = randomGenerator.Next(10)+1;
worker w = new worker(wTime, resets[i]);
Thread thrd1 = new Thread(new ThreadStart(w.work));
thrd1.Start();
}
WaitHandle.WaitAll(resets);
Console.WriteLine("ALL worker done - main exiting.");
}
}
public class worker
{
public string name;
public int wTime;
public AutoResetEvent mEvent;
public worker(int w, AutoResetEvent m)
{
name = w.ToString();
wTime = w * 1000;
mEvent = m;
}
public void work()
{
Console.WriteLine(name + " worker thread waiting for " + wTime + "....");
Thread.Sleep(wTime);
Console.WriteLine(name + " worker thread back...");
mEvent.Set();
}
}
}
簡單來說就是,開了5個線程,每個線程隨機休眠若干秒,都完成后通知主線程退出,這里就開了一個AutoResetEvent數(shù)組,主線程就WaitHandle.WaitAll(resets) ,子線程休眠完后就Set1個AutoResetEvent,最后都Set完后,主線程就會往下執(zhí)行。最后最后再來個買書付款取貨的例子,加深理解:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Program
{
const int numIterations = 10;
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static AutoResetEvent ChangeEvent = new AutoResetEvent(false);
//static ManualResetEvent myResetEvent = new ManualResetEvent(false);
//static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
static int number; //這是關鍵資源
static void Main()
{
Thread payMoneyThread = new Thread(new ThreadStart(PayMoneyProc));
payMoneyThread.Name = "付錢線程";
Thread getBookThread = new Thread(new ThreadStart(GetBookProc));
getBookThread.Name = "取書線程";
payMoneyThread.Start();
getBookThread.Start();
for (int i = 1; i <= numIterations; i++)
{
Console.WriteLine("買書線程:數(shù)量{0}", i);
number = i;
//Signal that a value has been written.
myResetEvent.Set();
//ChangeEvent.Set();
Thread.Sleep(10);
}
payMoneyThread.Abort();
getBookThread.Abort();
}
static void PayMoneyProc()
{
while (true)
{
myResetEvent.WaitOne();
//myResetEvent.Reset();
Console.WriteLine("{0}:數(shù)量{1}", Thread.CurrentThread.Name, number);
ChangeEvent.Set();
}
}
static void GetBookProc()
{
while (true)
{
ChangeEvent.WaitOne();
//ChangeEvent.Reset();
Console.WriteLine("{0}:數(shù)量{1}", Thread.CurrentThread.Name, number);
Console.WriteLine("------------------------------------------");
//Thread.Sleep(0);
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用MSScriptControl 在 C# 中讀取json數(shù)據(jù)的方法
下面小編就為大家?guī)硪黄褂肕SScriptControl 在 C# 中讀取json數(shù)據(jù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
.net C# 實現(xiàn)任意List的笛卡爾乘積算法代碼
笛卡爾(Descartes)乘積又叫直積。假設集合A={a,b},集合B={0,1,2},則兩個集合的笛卡爾積為{(a,0),(a,1),(a,2),(b,0),(b,1), (b,2)}。2013-05-05

