C# 多线程系列之Semaphore使用
Semaphore,即信号量的意思。是操作系统原始提供的内核同步对象。
Semaphore semaphoreAcceptedClients = new Semaphore(, 3,"Semaphore1");
解释一下意思:
第一个参数为:initialCount ,意指初始数量。Semaphore这个对象的使用是这么回事:在initialCoun设置为0的时候,WaitOne()方法会直接阻塞。至饿到它的Release方法被调用位置。但是如果initialCount>0,那么一开始便不会阻塞WaitOne方法N次。
第二个参数为maximumCount,即最大并发数。
第三个参数的话,我刚说它是操作系统的内核对象,那么又是可以用作跨进程线程同步的。
MSDN:http://msdn.microsoft.com/en-us/library/system.threading.semaphore(v=vs.110).aspx
举例:
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(, ); // Create and start five numbered threads.
//
for(int i = ; i <= ; 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(); // 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(); 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, ); 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( + padding); Console.WriteLine("Thread {0} releases the semaphore.", num);
Console.WriteLine("Thread {0} previous semaphore count: {1}",
num, _pool.Release());
}
}
跨进程的同步的话
A进程:
Semaphore semaphoreAcceptedClients = new Semaphore(, ,"Semaphore1");
B进程:
Semaphore semaphoreAcceptedClients2 = Semaphore.OpenExisting("Semaphore1");
使用和单进程中的线程同步一致。
完毕。
C# 多线程系列之Semaphore使用的更多相关文章
- Android进阶——多线程系列之Semaphore、CyclicBarrier、CountDownLatch
今天向大家介绍的是多线程开发中的一些辅助类,他们的作用无非就是帮助我们让多个线程按照我们想要的执行顺序来执行.如果我们按照文字来理解Semaphore.CyclicBarrier.CountDownL ...
- java多线程系列:Semaphore和Exchanger
本篇文章将介绍Semaphore和Exchanger这两个并发工具类. Semaphore 信号量(英语:Semaphore)又称为信号标,是一个同步对象,用于保持在0至指定最大值之间的一个计数值.当 ...
- Java多线程系列--“JUC锁”11之 Semaphore信号量的原理和示例
概要 本章,我们对JUC包中的信号量Semaphore进行学习.内容包括:Semaphore简介Semaphore数据结构Semaphore源码分析(基于JDK1.7.0_40)Semaphore示例 ...
- Java多线程系列--“JUC锁”03之 公平锁(一)
概要 本章对“公平锁”的获取锁机制进行介绍(本文的公平锁指的是互斥锁的公平锁),内容包括:基本概念ReentrantLock数据结构参考代码获取公平锁(基于JDK1.7.0_40)一. tryAcqu ...
- Java多线程系列--“JUC锁”01之 框架
本章,我们介绍锁的架构:后面的章节将会对它们逐个进行分析介绍.目录如下:01. Java多线程系列--“JUC锁”01之 框架02. Java多线程系列--“JUC锁”02之 互斥锁Reentrant ...
- Java多线程系列目录(共43篇)
最近,在研究Java多线程的内容目录,将其内容逐步整理并发布. (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线 ...
- Java多线程系列--“JUC锁”07之 LockSupport
概述 本章介绍JUC(java.util.concurrent)包中的LockSupport.内容包括:LockSupport介绍LockSupport函数列表LockSupport参考代码(基于JD ...
- Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock
概要 Java的JUC(java.util.concurrent)包中的锁包括"独占锁"和"共享锁".在“Java多线程系列--“JUC锁”02之 互斥锁Ree ...
- Java多线程系列
一.参考文献 1.:Java多线程系列目录 (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式 03. ...
随机推荐
- documeant 学习总结(二)
(一)移除节点及属性 /**移除节点和属性的操作 * @throws DocumentException */ public void RemoveOperator() ...
- 【Selenium专题】WebDriver启动firefox浏览器
firefox浏览器不需要下载驱动,原生支持,以下是代码运行环境,firefox启动封装在方法startFirefox()中 import org.openqa.selenium.WebDriver; ...
- 为什么要使用rem
为什么要使用rem 今天2019年4月16号更新,模仿网易移动端的的写法: html { font-size: 13.33333vw } @media screen and (max-width:32 ...
- 编译的 Ruby 2.3.0 缺少 openssl 支持的解决方法 (已解决)
我的系统是centos 7.5,已离线安装ruby-2.3.0,openssl-1.0.2l,rubygems-2.7.4 如下图: 但是在 gem sources -a http://gems.r ...
- jquery源码解析:jQuery工具方法when详解
我们先来看when方法是如何使用的: var cb = $.when(); //when方法也是返回一个延迟对象,源码是return deferred.promise();返回的延迟对象不能修改状 ...
- leetcode-766-Toeplitz Matrix(每一条对角线元素的比较)
题目描述: A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...
- Hibernate 使用注释书写配置文件
Hibernate 使用注释 Hibernate使用注释有个好处就是我们不需要建立.hbm.xml文件,直接在实体类中添加注解就可以完成往数据库中进行数据操作 配置文件:hibernate.cfg.x ...
- Linux安装Elasticsearch-head插件
首先需要下载以下内容: 我试验的对应版本:ES:elasticsearch-6.6.1.tar.gz Node:node-v10.15.3-linux-x64.tar JDK:jdk-8u201- ...
- excel批量中英翻译单元格
英 ==> 中 =FILTERXML(WEBSERVICE("http://fanyi.youdao.com/translate?&i="&H1&&q ...
- centeros7安装docker
一.官方安装 https://docs.docker.com/install/linux/docker-ce/centos/#upgrade-docker-after-using-the-conven ...