本文摘自PHP中文网,作者黄舟,侵删。
这篇文章主要为大家详细介绍了C#多线程之Semaphore用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下Semaphore:可理解为允许线程执行信号的池子,池子中放入多少个信号就允许多少线程同时执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | private static void MultiThreadSynergicWithSemaphore()
{
Semaphore semaphore = new Semaphore(0, 1);
Thread thread1 = new Thread(() =>
{
semaphore.WaitOne();
Console.WriteLine( "thread1 work" );
Thread.Sleep(5000);
semaphore.Release();
});
Thread thread2 = new Thread(() =>
{
semaphore.WaitOne();
Console.WriteLine( "thread2 work" );
Thread.Sleep(5000);
semaphore.Release();
});
thread2.Start();
thread1.Start();
semaphore.Release(1);
}
|
说明:
阅读剩余部分
相关阅读 >>
ants performance profiler(.net性能调优教程)
.net core中如何使用ref和span<t>提高程序性能的实现代码
详细介绍.net并行与多线程学习基础
关于.net c# sql数据库sqlhelper类实例代码
c#集合类有哪些?
.net实现简易的文件增量备份程序
asp .net 面试题及答案分享
.net core 和 .net .framework 相比哪个速度快?
c#如何连接到sql server2008数据库的示例分享
c#实现操作字符串的方法总结
更多相关阅读请进入《Semaphore》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » C#多线程之Semaphore的使用详解