码迷,mamicode.com
首页 > 其他好文 > 详细

lock模拟CountDownEvent

时间:2016-11-28 00:01:58      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:for   space   div   new   sea   cat   thread   log   readonly   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication44
{
    class Class2
    {
        public readonly static object _locker = new object();
        public static bool _singal;
        public static int _count;
        public static void Main()
        {
            _count = 3;
            for (int i = 1; i <= 3; i++)
            {

                new Thread(DoWork).Start(i);
            }
            Wait();
            Console.WriteLine("Compelete!");
            Console.ReadKey();

        }
        public static void DoWork(object i)
        {

            Set();
            Console.WriteLine(i);


        }

        public static void Set()
        {
            lock (_locker)
            {
                _count--;
                Monitor.PulseAll(_locker);
            }

        }
        public static void Wait()
        {
            lock (_locker)
            {
                while (_count > 0) { Monitor.Wait(_locker); }
            }
        }

    }
}

 

lock模拟CountDownEvent

标签:for   space   div   new   sea   cat   thread   log   readonly   

原文地址:http://www.cnblogs.com/kexb/p/6107190.html

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