码迷,mamicode.com
首页 > 编程语言 > 详细

多线程10-SemaphoreSlim

时间:2016-06-20 20:23:07      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

    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();
        }
    }

多线程10-SemaphoreSlim

标签:

原文地址:http://www.cnblogs.com/shidengyun/p/5601638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!