示例

下面的代码示例创建一个信号量,其最大计数为3,初始计数为零。 该示例启动五个线程,这会阻止等待信号量。 主线程使用 Release(Int32) 方法重载将信号量计数增加到其最大值,从而允许三个线程进入信号量。 每个线程使用 Thread.Sleep 方法等待一秒,以模拟工作,然后调用 Release() 方法重载以释放信号量。 每次释放信号灯时,都将显示以前的信号量计数。 控制台消息跟踪信号量使用。 每个线程的模拟工作时间间隔略有增加,使输出更易于读取。

C# 复制

 
using System;
using System.Threading; public class Example
{
// A semaphore that simulates a limited resource pool.
//
private static Semaphore _pool; // A padding interval to make the output more orderly.
private static int _padding; public static void Main()
{
// Create a semaphore that can satisfy up to three
// concurrent requests. Use an initial count of zero,
// so that the entire semaphore count is initially
// owned by the main program thread.
//
_pool = new Semaphore(0, 3); // Create and start five numbered threads.
//
for(int i = 1; i <= 5; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(Worker)); // Start the thread, passing the number.
//
t.Start(i);
} // Wait for half a second, to allow all the
// threads to start and to block on the semaphore.
//
Thread.Sleep(500); // The main thread starts out holding the entire
// semaphore count. Calling Release(3) brings the
// semaphore count back to its maximum value, and
// allows the waiting threads to enter the semaphore,
// up to three at a time.
//
Console.WriteLine("Main thread calls Release(3).");
_pool.Release(3); Console.WriteLine("Main thread exits.");
} private static void Worker(object num)
{
// Each worker thread begins by requesting the
// semaphore.
Console.WriteLine("Thread {0} begins " +
"and waits for the semaphore.", num);
_pool.WaitOne(); // A padding interval to make the output more orderly.
int padding = Interlocked.Add(ref _padding, 100); Console.WriteLine("Thread {0} enters the semaphore.", num); // The thread's "work" consists of sleeping for
// about a second. Each thread "works" a little
// longer, just to make the output more orderly.
//
Thread.Sleep(1000 + padding); Console.WriteLine("Thread {0} releases the semaphore.", num);
Console.WriteLine("Thread {0} previous semaphore count: {1}",
num, _pool.Release());
}
}

注解

使用 Semaphore 类控制对资源池的访问。 线程通过调用从类继承的方法进入信号量, WaitOne WaitHandle 并通过调用方法释放信号量 Release

每次线程进入信号量时,信号量的计数都将减少,并在线程释放信号量时递增。 如果计数为零,则后续请求会阻塞,直到其他线程释放信号量。 当所有线程都已释放信号量后,计数将达到创建信号量时指定的最大值。

Semaphore 类 的使用理解C#的更多相关文章

  1. 转 关于C#中派生类调用基类构造函数的理解

    关于C#中派生类调用基类构造函数的理解 .c#class       本文中的默认构造函数是指在没有编写构造函数的情况下系统默认的无参构造函数 1.  当基类中没有自己编写构造函数时,派生类默认的调用 ...

  2. C++中 类的构造函数理解(一)

    C++中 类的构造函数理解(一) 写在前面 这段时间完成三个方面的事情: 1.继续巩固基础知识(主要是C++ 方面的知识) 2.尝试实现一个iOS的app,通过完成app,学习iOS开发中要用到的知识 ...

  3. (转)regex类(个人理解)

    regex类(个人理解)   C#regex是正则表达式类用于string的处理,查找匹配的字符串.1,先看一个例子Regex regex=new Regex(@”OK“)://我们要在目标字符串中找 ...

  4. 浅谈Semaphore类

    Semaphore类有两个重要方法 1.semaphore.acquire(); 请求一个信号量,这时候信号量个数-1,当减少到0的时候,下一次acquire不会再执行,只有当执行一个release( ...

  5. 类之间关系理解:组合>聚合>关联>依赖;实现,继承

    类之间关系理解:组合>聚合>关联>依赖:实现,继承 1. 从类之间的关系来看,不外乎以下几种 组合>聚合>关联>依赖:实现,继承 且可以分为以下两类: (1)实现, ...

  6. Java中Class和单例类的作用与类成员的理解

    Java中Class类的作用与深入理解 在程序运行期间,Java运行时系统始终为所有的对象维护一个被称为运行时的类型标识.这个信息跟踪着每个对象所属的类.JVM利用运行时信息选择相应的方法执行.而保存 ...

  7. 浅谈Semaphore类-示例

    Semaphore类有两个重要方法 1.semaphore.acquire(); 请求一个信号量,这时候信号量个数-1,当减少到0的时候,下一次acquire不会再执行,只有当执行一个release( ...

  8. python中元类(metaclass)的理解

    原文地址:http://www.cnblogs.com/tkqasn/p/6524879.html 一:类也是对象 类就是一组用来描述如何生成一个对象的代码. 类也是一个对象,只要你使用关键字clas ...

  9. 线程同步工具 Semaphore类的基础使用

    推荐好文: 线程同步工具(一) 线程同步工具(二)控制并发访问多个资源 并发工具类(三)控制并发线程数的Semaphore 简介 Semaphore是基于计数的信号量,可以用来控制同时访问特定资源的线 ...

随机推荐

  1. 动态路由协议、RIP

    动态路由协议.RIP      一.动态路由协议        1)动态路由协议概述        2)度量值        3)收敛        4)静态路由与动态路由的比较        5)动 ...

  2. endnote x9.3.3 for windows安装教程

    EndNote X9.3.3 是一款非常nice的实用型文献管理软件,EndNote X9功能极其强劲,便捷好用.本文提供EndNote X9.3.3安装破解激活教程.方法,内附EndNote x9. ...

  3. jvm源码解读--12 invokspecial指令的解读

    先看代码 package com.zyt.jvmbook; public class Girl extends Person{ public Girl() { int a; } @Override p ...

  4. Tr0ll靶机

    一.主机探测 二.信息收集 进入21端口 发现文件并下载 下载文件 作为字典进行登录爆破 用字典爆破 ssh登录 查找信息   /etc/init.d/ssh start scp root@192.1 ...

  5. REM 根据卷标搜索随身固态U盘的盘符.BAT

    REM 根据卷标搜索随身固态U盘的盘符.BAT@echo offfor /f "tokens=2 delims==" %%a in ('wmic logicaldisk where ...

  6. Mac卸载软件真不省心啊

    最近看看磁盘觉得有点小, 就整理了一下, 经过一番折腾, 发现MacOS卸载软件可真是不省心啊. 从应用里移到垃圾桶仅仅是第一步, 当然对于不读写任何文件的应用也许就可以了. 咱们看看赶紧卸载一个软件 ...

  7. 「必知必会」最细致的 LinkedList 原理分析

    1.结构 1. 继承   该类继承自 AbstractSequentialList 这个是由于他是一个顺序的列表,所以说继承的是一个顺序的 List 2. 实现 这个类实现的接口比较多,具体如下: 首 ...

  8. Android面试官:说说你对 Binder 驱动的了解?

    面试官提了一个问题:说说你对 binder 驱动的了解.这个问题虽有些 "面试造火箭" 的无奈,可难点就是亮点.价值所在,是筛选面试者的有效手段.如果让你回答,你能说出多少呢?我们 ...

  9. noip模拟35[第一次4题·裂了]

    noip模拟35 solutions 这是我第一次这么正式的考四个题,因为这四个题都出自同一个出题人,并不是拼盘拼出来的. 但是考得非常的不好,因为题非常难而且一直想睡觉.. 有好多我根本就不会的算法 ...

  10. 《手把手教你》系列技巧篇(十六)-java+ selenium自动化测试-元素定位大法之By xpath下卷(详细教程)

    1.简介 按宏哥计划,本文继续介绍WebDriver关于元素定位大法,这篇介绍定位倒数二个方法:By xpath.xpath 的定位方法, 非常强大.  使用这种方法几乎可以定位到页面上的任意元素. ...