zoukankan      html  css  js  c++  java
  • 线程同步:旗语(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. }  
  • 相关阅读:
    网页色彩搭配
    jquery富文本在线编辑器UEditor
    编写、部署、应用JavaBean
    图片存储构架学习
    JVM内存管理(一)
    同步or异步
    分库分表策略的可实现架构
    百万数据查询优化技巧三十则
    匿名内部类
    MySQL limit分页查询优化写法
  • 原文地址:https://www.cnblogs.com/sunwei2012/p/1868280.html
Copyright © 2011-2022 走看看