zoukankan      html  css  js  c++  java
  • 多线程10-SemaphoreSlim

        class Program
        {
            static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(2);
            static void Main()
            {
                for(int i=0;i<=6;i++)
                {
                    string threadName = "Thread" + i;
                    int secondsToWait = 2 + 2 * i;
                    var t = new Thread(() => ShowMessage(threadName, secondsToWait));
                    t.Start();
                }
            }
            static void ShowMessage(string name,int second)
            {
                Console.WriteLine("{0} watis to showmessage", name);
                semaphoreSlim.Wait();
                Console.WriteLine("{0} was showmessage", name);
                Thread.Sleep(TimeSpan.FromSeconds(5));
                Console.WriteLine("{0} is completed", name);
                semaphoreSlim.Release();
            }
        }
  • 相关阅读:
    JAVA中的类和对象
    JAVA方法
    JAVA数组
    JAVA流程控制语句
    JAVA常用的运算符
    JAVA中的变量和常量
    JAVA安装及环境变量配置
    linux环境下配置jmeter环境变量
    linux环境下解压文件
    安装程序遇到错误0x80240037
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5601638.html
Copyright © 2011-2022 走看看