Semaphore,即信号量的意思。是操作系统原始提供的内核同步对象。

  1. 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

举例:

  1. using System;
  2. using System.Threading;
  3.  
  4. public class Example
  5. {
  6. // A semaphore that simulates a limited resource pool.
  7. //
  8. private static Semaphore _pool;
  9.  
  10. // A padding interval to make the output more orderly.
  11. private static int _padding;
  12.  
  13. public static void Main()
  14. {
  15. // Create a semaphore that can satisfy up to three
  16. // concurrent requests. Use an initial count of zero,
  17. // so that the entire semaphore count is initially
  18. // owned by the main program thread.
  19. //
  20. _pool = new Semaphore(, );
  21.  
  22. // Create and start five numbered threads.
  23. //
  24. for(int i = ; i <= ; i++)
  25. {
  26. Thread t = new Thread(new ParameterizedThreadStart(Worker));
  27.  
  28. // Start the thread, passing the number.
  29. //
  30. t.Start(i);
  31. }
  32.  
  33. // Wait for half a second, to allow all the
  34. // threads to start and to block on the semaphore.
  35. //
  36. Thread.Sleep();
  37.  
  38. // The main thread starts out holding the entire
  39. // semaphore count. Calling Release(3) brings the
  40. // semaphore count back to its maximum value, and
  41. // allows the waiting threads to enter the semaphore,
  42. // up to three at a time.
  43. //
  44. Console.WriteLine("Main thread calls Release(3).");
  45. _pool.Release();
  46.  
  47. Console.WriteLine("Main thread exits.");
  48. }
  49.  
  50. private static void Worker(object num)
  51. {
  52. // Each worker thread begins by requesting the
  53. // semaphore.
  54. Console.WriteLine("Thread {0} begins " +
  55. "and waits for the semaphore.", num);
  56. _pool.WaitOne();
  57.  
  58. // A padding interval to make the output more orderly.
  59. int padding = Interlocked.Add(ref _padding, );
  60.  
  61. Console.WriteLine("Thread {0} enters the semaphore.", num);
  62.  
  63. // The thread's "work" consists of sleeping for
  64. // about a second. Each thread "works" a little
  65. // longer, just to make the output more orderly.
  66. //
  67. Thread.Sleep( + padding);
  68.  
  69. Console.WriteLine("Thread {0} releases the semaphore.", num);
  70. Console.WriteLine("Thread {0} previous semaphore count: {1}",
  71. num, _pool.Release());
  72. }
  73. }

跨进程的同步的话

A进程:

  1. Semaphore semaphoreAcceptedClients = new Semaphore(, ,"Semaphore1");

B进程:

  1. Semaphore semaphoreAcceptedClients2 = Semaphore.OpenExisting("Semaphore1");

使用和单进程中的线程同步一致。

完毕。

C# 多线程系列之Semaphore使用的更多相关文章

  1. Android进阶——多线程系列之Semaphore、CyclicBarrier、CountDownLatch

    今天向大家介绍的是多线程开发中的一些辅助类,他们的作用无非就是帮助我们让多个线程按照我们想要的执行顺序来执行.如果我们按照文字来理解Semaphore.CyclicBarrier.CountDownL ...

  2. java多线程系列:Semaphore和Exchanger

    本篇文章将介绍Semaphore和Exchanger这两个并发工具类. Semaphore 信号量(英语:Semaphore)又称为信号标,是一个同步对象,用于保持在0至指定最大值之间的一个计数值.当 ...

  3. Java多线程系列--“JUC锁”11之 Semaphore信号量的原理和示例

    概要 本章,我们对JUC包中的信号量Semaphore进行学习.内容包括:Semaphore简介Semaphore数据结构Semaphore源码分析(基于JDK1.7.0_40)Semaphore示例 ...

  4. Java多线程系列--“JUC锁”03之 公平锁(一)

    概要 本章对“公平锁”的获取锁机制进行介绍(本文的公平锁指的是互斥锁的公平锁),内容包括:基本概念ReentrantLock数据结构参考代码获取公平锁(基于JDK1.7.0_40)一. tryAcqu ...

  5. Java多线程系列--“JUC锁”01之 框架

    本章,我们介绍锁的架构:后面的章节将会对它们逐个进行分析介绍.目录如下:01. Java多线程系列--“JUC锁”01之 框架02. Java多线程系列--“JUC锁”02之 互斥锁Reentrant ...

  6. Java多线程系列目录(共43篇)

    最近,在研究Java多线程的内容目录,将其内容逐步整理并发布. (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线 ...

  7. Java多线程系列--“JUC锁”07之 LockSupport

    概述 本章介绍JUC(java.util.concurrent)包中的LockSupport.内容包括:LockSupport介绍LockSupport函数列表LockSupport参考代码(基于JD ...

  8. Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock

    概要 Java的JUC(java.util.concurrent)包中的锁包括"独占锁"和"共享锁".在“Java多线程系列--“JUC锁”02之 互斥锁Ree ...

  9. Java多线程系列

    一.参考文献 1.:Java多线程系列目录 (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式 03. ...

随机推荐

  1. 如何使用安卓4.4的SD卡?

    安卓4.4默认情况下,后安装的程序无权写入数据到SD卡中,那么是否我们就不能用了?看了很多文章,都说要Root,随后修改配置文件.我觉得这不是很好的方法,Root之后的安卓会有很大风险,这不是最好的办 ...

  2. leecode刷题(15)-- 验证回文字符串

    leecode刷题(15)-- 验证回文字符串 验证回文字符串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 ...

  3. scrapy框架基于CrawlSpider的全站数据爬取

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  4. Windows Server 2008 R2 下 Core界面

    Windows Server 2008 R2 下 Core界面 关于 sc 以及 net 命令 Sc 命令较不全面,仅仅是给服务发送一个开启或者关闭就结束了 Net 命令比较安全,它监视了整个服务的启 ...

  5. linux修改用户名

      貌似只需要改红色的就行了,要保险就都改,比如阿里云服务器就要2个都改   ubuntu:修改 /etc/hostname               修改 /etc/hosts  比如:127.0 ...

  6. js中的valueOf与toString

    所有对象继承了两个转换方法: 第一个是toString(),它的作用是返回一个反映这个对象的字符串 第二个是valueOf(),它的作用是返回它相应的原始值 一般来说,对象到字符串的转换经过了如下步骤 ...

  7. JavaWeb学习笔记(十八)—— DBUtils的使用

    一.DBUtils概述 1.1 什么是DBUtils commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbuti ...

  8. Laravel 控制器 Controller 传值到 视图 View 的几种方法总结

    单个值的传递   with public function index() { $test = "测试"; return view('test.index')->with(' ...

  9. Python——模块以及导入注意事项

    在Python中,每一个文件都应该是可以被导入的. 每一个独立的python文件都是一个模块 在导入文件时,文件中所有没有任何缩进的代码都会被执行一遍. 而在实际应用时,每个模块都是有专人负责独立开发 ...

  10. PIE SDK 距离分类和最大似然分类

       1.算法功能简介 监督分类,也叫训练场地法.训练分类法,是遥感图像分类的一种,用被确认类别的样本像元去识别其他未知类别像元的过程.监督分类算法有平行算法.平行六面体法.最小距离法.最大似然法.马 ...