免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
C# Mutex對象的使用

C#語言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C# Mutex對象,包括介紹控制好多個線程相互之間的聯(lián)系等方面。

如何控制好多個線程相互之間的聯(lián)系,不產(chǎn)生沖突和重復(fù),這需要用到互斥對象,即:System.Threading 命名空間中的 Mutex 類。

我們可以把Mutex看作一個出租車,乘客看作線程。乘客首先等車,然后上車,最后下車。當(dāng)一個乘客在車上時,其他乘客就只有等他下車以后才可以上車。而線程與C# Mutex對象的關(guān)系也正是如此,線程使用Mutex.WaitOne()方法等待C# Mutex對象被釋放,如果它等待的C# Mutex對象被釋放了,它就自動擁有這個對象,直到它調(diào)用Mutex.ReleaseMutex()方法釋放這個對象,而在此期間,其他想要獲取這個C# Mutex對象的線程都只有等待。

下面這個例子使用了C# Mutex對象來同步四個線程,主線程等待四個線程的結(jié)束,而這四個線程的運(yùn)行又是與兩個C# Mutex對象相關(guān)聯(lián)的。

其中還用到AutoResetEvent類的對象,可以把它理解為一個信號燈。這里用它的有信號狀態(tài)來表示一個線程的結(jié)束。

  • using System;  
  • using System.Threading;  
  •  
  • namespace ThreadExample  
  • {  
  • public class MutexSample  
  • {  
  • static Mutex gM1;  
  • static Mutex gM2;  
  • const int ITERS = 100;  
  • static AutoResetEvent Event1 = new AutoResetEvent(false);  
  • static AutoResetEvent Event2 = new AutoResetEvent(false);  
  • static AutoResetEvent Event3 = new AutoResetEvent(false);  
  • static AutoResetEvent Event4 = new AutoResetEvent(false);  
  •  
  • public static void Main(String[] args)  
  • {  
  • Console.WriteLine("Mutex Sample ");  
  • //創(chuàng)建一個Mutex對象,并且命名為MyMutex  
  • gM1 = new Mutex(true,"MyMutex");  
  • //創(chuàng)建一個未命名的Mutex 對象.  
  • gM2 = new Mutex(true);  
  • Console.WriteLine(" - Main Owns gM1 and gM2");  
  •  
  • AutoResetEvent[] evs = new AutoResetEvent[4];  
  • evs[0] = Event1; //為后面的線程t1,t2,t3,t4定義AutoResetEvent對象  
  • evs[1] = Event2;   
  • evs[2] = Event3;   
  • evs[3] = Event4;   
  •  
  • MutexSample tm = new MutexSample( );  
  • Thread t1 = new Thread(new ThreadStart(tm.t1Start));  
  • Thread t2 = new Thread(new ThreadStart(tm.t2Start));  
  • Thread t3 = new Thread(new ThreadStart(tm.t3Start));  
  • Thread t4 = new Thread(new ThreadStart(tm.t4Start));  
  • t1.Start( );// 使用Mutex.WaitAll()方法等待一個Mutex數(shù)組中的對象全部被釋放  
  • t2.Start( );// 使用Mutex.WaitOne()方法等待gM1的釋放  
  • t3.Start( );// 使用Mutex.WaitAny()方法等待一個Mutex數(shù)組中任意一個對象被釋放  
  • t4.Start( );// 使用Mutex.WaitOne()方法等待gM2的釋放  
  •  
  • Thread.Sleep(2000);  
  • Console.WriteLine(" - Main releases gM1");  
  • gM1.ReleaseMutex( ); //線程t2,t3結(jié)束條件滿足  
  •  
  • Thread.Sleep(1000);  
  • Console.WriteLine(" - Main releases gM2");  
  • gM2.ReleaseMutex( ); //線程t1,t4結(jié)束條件滿足  
  •  
  • //等待所有四個線程結(jié)束  
  • WaitHandle.WaitAll(evs);   
  • Console.WriteLine(" Mutex Sample");  
  • Console.ReadLine();  
  • }  
  •  
  • public void t1Start( )  
  • {  
  • Console.WriteLine("t1Start started, Mutex.WaitAll(Mutex[])");  
  • Mutex[] gMs = new Mutex[2];  
  • gMs[0] = gM1;//創(chuàng)建一個Mutex數(shù)組作為Mutex.WaitAll()方法的參數(shù)  
  • gMs[1] = gM2;  
  • Mutex.WaitAll(gMs);//等待gM1和gM2都被釋放  
  • Thread.Sleep(2000);  
  • Console.WriteLine("t1Start finished, Mutex.WaitAll(Mutex[]) satisfied");  
  • Event1.Set( ); //線程結(jié)束,將Event1設(shè)置為有信號狀態(tài)  
  • }  
  • public void t2Start( )  
  • {  
  • Console.WriteLine("t2Start started, gM1.WaitOne( )");  
  • gM1.WaitOne( );//等待gM1的釋放  
  • Console.WriteLine("t2Start finished, gM1.WaitOne( ) satisfied");  
  • Event2.Set( );//線程結(jié)束,將Event2設(shè)置為有信號狀態(tài)  
  • }  
  • public void t3Start( )  
  • {  
  • Console.WriteLine("t3Start started, Mutex.WaitAny(Mutex[])");  
  • Mutex[] gMs = new Mutex[2];  
  • gMs[0] = gM1;//創(chuàng)建一個Mutex數(shù)組作為Mutex.WaitAny()方法的參數(shù)  
  • gMs[1] = gM2;  
  • Mutex.WaitAny(gMs);//等待數(shù)組中任意一個Mutex對象被釋放  
  • Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])");  
  • Event3.Set( );//線程結(jié)束,將Event3設(shè)置為有信號狀態(tài)  
  • }  
  • public void t4Start( )  
  • {  
  • Console.WriteLine("t4Start started, gM2.WaitOne( )");  
  • gM2.WaitOne( );//等待gM2被釋放  
  • Console.WriteLine("t4Start finished, gM2.WaitOne( )");  
  • Event4.Set( );//線程結(jié)束,將Event4設(shè)置為有信號狀態(tài)  
  • }  
  • }  

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#多線程
C#中的線程(二) 線程同步基礎(chǔ)
C# 溫故而知新: 線程篇(四)
C# 實(shí)現(xiàn)多線程的同步方法詳解
C #中的幾個線程同步對象方法
多線程的應(yīng)用
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服