线程同步:旗语(Semaphore)

原创  线程同步:旗语(Semaphore) 收藏

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. //添加命名空间  
  6. using System.Threading;  
  7. using System.Diagnostics;  
  8.   
  9. //旗语(Semaphore)锁定非常类似于互斥锁定,其区别是,旗语锁定可以同时由多个线程使用。  
  10. //旗语锁定是一种计数的互斥的锁定  
  11. //使用旗语锁定,可以定义允许同时访问受旗语访锁定保护的资源的线程个数。如果有许多资源,且只允许一定数量的线程访问该资源,就可以使用旗语锁定  
  12. namespace ThreadStudy2  
  13. {  
  14.     class Program  
  15.     {  
  16.         static void Main(string[] args)  
  17.         {  
  18.             int threadCount = 6;  
  19.             int semaphoreCount = 4;  
  20.             //第一参数:最初自由的锁定数,第二参数:锁定数的计数  
  21.             Semaphore semaphore = new Semaphore(semaphoreCount,semaphoreCount);  
  22.             Thread[] threads = new Thread[threadCount];  
  23.             for (int i = 0; i < threadCount; i++)  
  24.             {  
  25.                 threads[i] = new Thread(ThreadMain);  
  26.                 threads[i].Start(semaphore);  
  27.             }  
  28.             for (int i = 0; i < threadCount; i++)  
  29.             {  
  30.                 threads[i].Join();  
  31.             }  
  32.             Console.WriteLine("All threads finished");  
  33.             Console.Read();  
  34.                  
  35.         }  
  36.         static void ThreadMain(object o)  
  37.         {  
  38.             //线程利用WaitOne()锁定了旗语,旗语锁定的计数是4,所以4个线程可以获得锁定。线程5必须等待,时间为500毫秒。如果在该等待时间后未能获得锁定,线程就把一个信息写入控制台,循环中继续等待  
  39.             //只要获得锁定,线程就把一个信息写入控制台,睡眠一段时间后,解除锁定,在解除锁定时,一定要解除资源的锁定。  
  40.             //这就是在finally处理程序中调用Release()方法的原因  
  41.             Semaphore semaphore = o as Semaphore;  
  42.             Trace.Assert(semaphore != null"o must be a Semaphore type");  
  43.             bool isCompleted = false;  
  44.             while (!isCompleted)  
  45.             {  
  46.                 if (semaphore.WaitOne(600, false))  
  47.                 {  
  48.                     try  
  49.                     {  
  50.                         Console.WriteLine("Thread {0} locks the sempahore",Thread.CurrentThread.ManagedThreadId);  
  51.                         Thread.Sleep(2000);  
  52.                     }  
  53.                     finally  
  54.                     {  
  55.                         semaphore.Release();  
  56.                         Console.WriteLine("Thread {0} releases the semaphore ",Thread.CurrentThread.ManagedThreadId);  
  57.                         isCompleted = true;  
  58.                     }  
  59.                 }  
  60.                 else  
  61.                 {  
  62.                     Console.WriteLine("Timeout for thread {0} ; wait again", Thread.CurrentThread.ManagedThreadId);  
  63.                 }  
  64.             }  
  65.         }  
  66.         //运行现象:  
  67.         //4个线程获得了锁定,另外2个线程需要等待,该等待会重复进行,直到4个获得锁定的线程之一解除了旗语锁定。  
  68.   
  69.     }  
  70. }  

 

转载于:https://www.cnblogs.com/sunwei2012/archive/2010/11/03/1868280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值