System.Threading.Mutex的使用方法

Mutex 这个类其实就是为共享资源加锁,比如说A、B、C三个人都去ATM机上取钱,只要有一个人占着机器取钱,那么另外两个人就必须在那等着。只有占着机器的人取完钱之后,其他人才能去占用机器取钱。

Mutex这个类有两种等待方法,一种是死等WaitOne(),另一种是等一会WaitOne(1000)。

WaitOne()死等的意思就是:我必须要取钱,哪怕前面人再多,时间再晚,我必须取钱,哪怕等到海枯石烂我也要等!

WaitOne(1000)等一会的意思是:我今天还有别的事情,我就等几分钟,这几分钟之内还轮不到我的话,我就走了,今天就不再取钱了。

结合上面我的描述,再去看看微软给的例子,就明白了:官方的Mutex使用例子  https://docs.microsoft.com/en-us/dotnet/api/system.threading.mutex?view=netframework-4.0
————————————————
版权声明:本文为CSDN博主「公众号:格局必须大」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Bi_ZhiAn/article/details/118784255

using System;
using System.Threading;

class Example
{
// Create a new Mutex. The creating thread does not own the mutex.
private static Mutex mut = new Mutex();
private const int numIterations = 1;
private const int numThreads = 3;

static void Main()
{
// Create the threads that will use the protected resource.
for(int i = 0; i < numThreads; i++)
{
Thread newThread = new Thread(new ThreadStart(ThreadProc));
newThread.Name = String.Format("Thread{0}", i + 1);
newThread.Start();
}

// The main thread exits, but the application continues to
// run until all foreground threads have exited.
}

private static void ThreadProc()
{
for(int i = 0; i < numIterations; i++)
{
UseResource();
}
}

// This method represents a resource that must be synchronized
// so that only one thread at a time can enter.
private static void UseResource()
{
// Wait until it is safe to enter.
Console.WriteLine("{0} is requesting the mutex",
Thread.CurrentThread.Name);
mut.WaitOne();

Console.WriteLine("{0} has entered the protected area",
Thread.CurrentThread.Name);

// Place code to access non-reentrant resources here.

// Simulate some work.
Thread.Sleep(500);

Console.WriteLine("{0} is leaving the protected area",
Thread.CurrentThread.Name);

// Release the Mutex.
mut.ReleaseMutex();
Console.WriteLine("{0} has released the mutex",
Thread.CurrentThread.Name);
}
}
// The example displays output like the following:
// Thread1 is requesting the mutex
// Thread2 is requesting the mutex
// Thread1 has entered the protected area
// Thread3 is requesting the mutex
// Thread1 is leaving the protected area
// Thread1 has released the mutex
// Thread3 has entered the protected area
// Thread3 is leaving the protected area
// Thread3 has released the mutex
// Thread2 has entered the protected area
// Thread2 is leaving the protected area
// Thread2 has released the mutex

 

using System;
using System.Threading;

class Example
{
// Create a new Mutex. The creating thread does not own the mutex.
private static Mutex mut = new Mutex();
private const int numIterations = 1;
private const int numThreads = 3;

static void Main()
{
Example ex = new Example();
ex.StartThreads();
}

private void StartThreads()
{
// Create the threads that will use the protected resource.
for(int i = 0; i < numThreads; i++)
{
Thread newThread = new Thread(new ThreadStart(ThreadProc));
newThread.Name = String.Format("Thread{0}", i + 1);
newThread.Start();
}

// The main thread returns to Main and exits, but the application continues to
// run until all foreground threads have exited.
}

private static void ThreadProc()
{
for(int i = 0; i < numIterations; i++)
{
UseResource();
}
}

// This method represents a resource that must be synchronized
// so that only one thread at a time can enter.
private static void UseResource()
{
// Wait until it is safe to enter, and do not enter if the request times out.
Console.WriteLine("{0} is requesting the mutex", Thread.CurrentThread.Name);
if (mut.WaitOne(1000)) {
Console.WriteLine("{0} has entered the protected area",
Thread.CurrentThread.Name);

// Place code to access non-reentrant resources here.

// Simulate some work.
Thread.Sleep(5000);

Console.WriteLine("{0} is leaving the protected area",
Thread.CurrentThread.Name);

// Release the Mutex.
mut.ReleaseMutex();
Console.WriteLine("{0} has released the mutex",
Thread.CurrentThread.Name);
}
else {
Console.WriteLine("{0} will not acquire the mutex",
Thread.CurrentThread.Name);
}
}

~Example()
{
mut.Dispose();
}
}
// The example displays output like the following:
// Thread1 is requesting the mutex
// Thread1 has entered the protected area
// Thread2 is requesting the mutex
// Thread3 is requesting the mutex
// Thread2 will not acquire the mutex
// Thread3 will not acquire the mutex
// Thread1 is leaving the protected area
// Thread1 has released the mutex

上一篇:JAVA虚拟机24-原子性、可见性与有序性、先行发生原则


下一篇:2021SC@SDUSC【软件工程应用与实践】Cocoon项目12——sitemap-impl文件夹分析(1)