首页 技术 正文
技术 2022年11月14日
0 收藏 867 点赞 4,887 浏览 3247 个字

<!–
.cnblogs_code {
background-color: #f5f5f5;
font-family: Courier New!important;
font-size: 12px!important;
border: 1px solid #ccc;
padding: 5px;
overflow: auto;
margin: 5px 0;
color: #000;
}
.cnblogs_fontbu{
color: royalblue;
}
.cnblogs_fontskbule{
color: lightskyblue;
}
.cnblogs_fontyellow{
color: #800000;
}
#cnblogs_post_body h1 {
margin: 10px 0;
font-family: ‘Microsoft Yahei’;
text-align: left;
padding: 6px 20px;
color: #fff;
background: #55895B;
font-size: 20px;
border-radius: 4px;
clear: both;
}
–>

使用Mutex类

                class Program
{
static void Main(string[] args)
{
const string MutexName ="CSharpThreadingCookbook";
using (var m = new Mutex(false, MutexName))
{
if (!m.WaitOne(TimeSpan.FromSeconds(5), false))
{
Console.WriteLine("Second instance is running!");
}
else {
Console.WriteLine("Runing!");
Console.ReadLine();
m.ReleaseMutex();
}
}
}
}

当主程序启动时,定义了一个指定名称的互斥量,设置initialowner标志为false。这意味着如果互斥量已经被创建,则允许程序获取该互斥量。如果没有获取到互斥量,程序则简单的显示running,的等待知道按下了任何键,然后释放该互斥量并退出。 如果再运行同样一个程序,则会在5秒内尝试获取互斥量。如果此时在第一个程序中按下了任何键,第二个程序则会开始执行。然而,如果保持等待5秒钟,第二个程序将无法获取到该互斥量。 该方式可用于在不同的程序中同步线程,可被推广到大量的使用场景中。

使用SemaphoreSilm类

            static SemaphoreSlim _semaphore = new SemaphoreSlim(4);            static void AccessDatabase(string name, int seconds) {
Console.WriteLine("{0} waits to access a database",name);
_semaphore.Wait();
Console.WriteLine("{0} was granted an access to a database",name);
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine("{0} is completed",name);
_semaphore.Release(); }
             static void Main(string[] args)
{
for (int i = 1; i <= 6; i++) {
string threadName ="Thread" + i;
int secondsToWait = 2 + 2 * i;
var t = new Thread(() => AccessDatabase(threadName, secondsToWait));
t.Start();
}
Console.ReadKey();
}

当主程序启动时,创建了SemaphoreSlim的一个实例,并在其构造函数中指定允许的并发线程数量。然后启动了6个不同名称和不同初始运行时间的线程。每个线程都尝试获取数据库的访问,但是我们借助于信号系统限制了访问数据库的并发数为4个线程。当有4个线程获取数据库的访问后,其他两个线程需要等待,直到之前线程中的某一个完成工作并调用_semaphore.Release方法来发出信号。

使用AutoResetEvent类

            private staticAutoResetEvent _workerEvent=new AutoResetEvent(false);
private staticAutoResetEvent _mainEvent =new AutoResetEvent(false);
static void Process(int seconds)
{
Console.WriteLine("Starting a long running work... ");
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine("Work is done!");
_workerEvent.Set();
Console.WriteLine("Waiting for a main thread to complete its work");
_mainEvent.WaitOne();
Console.WriteLine("starting second operation... ");
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine("Work is done!");
_workerEvent.Set();
}
            static void Main(string[] args)
{
var t = new Thread(() => Process(10));
t.Start();
Console.WriteLine("Waiting for a main thread to complete its work");
_workerEvent.WaitOne();
Console.WriteLine("First operation is completed!");
Console.WriteLine("Performing an operation on a main thread");
Thread.Sleep(TimeSpan.FromSeconds(5));
_mainEvent.Set();
Console.WriteLine("Now running the second operation on a second thread");
_workerEvent.WaitOne();
Console.WriteLine("Second operation is completed!");
}

当主程序启动时,定义了两个autoresetEvent实例。其中一个是从子线程向主线程发信号,另一个实例是从主线程向子线程发信息号 。我们向AutoResetEvent构造方法传入false,定义了这两个实例初始状态为unsignaled。这意味着我们任何线程调用这两个对象中的任何一个 waitone方法将会被堵塞,直到我们调用了set方法。如果初试事件为true,那么autoresetEvent实例的状态为sigaled,如果线程调用waitone方法则会立即处理。 然后事件状态自动变为unsignaled,所以需要再对改实例调用一次set方法,以便让其他的线程对该实例调用waitone方法从而继续执行。 然后我们创建了第二个线程,其会执行第一个操作10秒钟,然后等待从第二个线程发出的信号。该信号意味着第一个操作已经完成。现在第二个线程在 等待主线程的信号,我们对主线程做了一些1附加工作,并通过调用_mainEvent.Set方法发送了一个信号。然后等待从第二个线程发出的另一个信号 AutoResetEvent类采用的是内核时间模式,所以等待时间不能太长。使用2.6节中的ManualResetEventslim类更好,因为他使用的是混合模式。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919