System.Threading.CountdownEvent  是一个同步基元,它在收到一定次数的信号之后,将会解除对其等待线程的锁定。 CountdownEvent  专门用于以下情况:您必须使用 ManualResetEvent 或 ManualResetEventSlim,并且必须在用信号通知事件之前手动递减一个变量。 例如,在分叉/联接方案中,您可以只创建一个信号计数为 5 的 CountdownEvent,然后在线程池上启动五个工作项,并且让每个工作项在完成时调用 Signal。 每次调用 Signal 时,信号计数都会递减 1。 在主线程上,对 Wait 的调用将会阻塞,直至信号计数为零。
CountdownEvent  具有这些附加功能:
•可通过使用取消标记来取消等待操作。
•创建实例之后可以递增它的信号计数。
•通过调用 Reset 方法,可在 Wait 返回之后重用实例。
•实例公开 WaitHandle 以便与其他 .NET Framework 同步 API(例如 WaitAll)进行集成。——MSDN

1.普通示例

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication18
{
    class Program
    {

        static CountdownEvent cde = null;
        /// <summary>
        /// 模拟建房子
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //建房子第一期工人
            string[] _bhPerson1 = new string[5] { "Yan", "Zhi", "wei", "Do", "Work" };
            //建房子第二期工人
            string[] _bhPerson2 = new string[3] { "Yan2", "Zhi2", "wei2" };
            //建房子第三期工人
            string[] _bhPerson3 = new string[3] { "Yan3", "Zhi3", "wei3" };

            using (cde = new CountdownEvent(Environment.ProcessorCount))//开始监管,相当于监工
            {
                cde.Reset(_bhPerson1.Length);//设置第一期建造需要5个人
                foreach (string person in _bhPerson1)
                {
                    Task.Factory.StartNew(() =>
                    {
                        BuilderHourseStep1(person);
                    });

                }
                cde.Wait();//等待第一期建造完成
                Console.WriteLine("-----------------------");

                cde.Reset(_bhPerson2.Length);//设置第二期建需要三个人
                foreach (string person in _bhPerson2)
                {
                    Task.Factory.StartNew(() =>
                      {
                          BuilderHourseStep2(person);
                      });
                }
                cde.Wait();//等待第二期建造完成
                Console.WriteLine("-----------------------");

                cde.Reset(_bhPerson3.Length);//设置第三期建需要三个人
                foreach (string person in _bhPerson3)
                {
                    Task.Factory.StartNew(() =>
                        {
                            BuilderHourseStep3(person);
                        });
                }
                cde.Wait();//等待第三期建造完成
                Console.WriteLine("-----------------------");
            }
        }

        /// <summary>
        /// 建房子第一道所需要的工序
        /// </summary>
        /// <param name="person"></param>
        static void BuilderHourseStep1(string person)
        {
            try
            {
                Console.WriteLine(string.Format("『{0}』BuilderHourseStep1....", person));
            }
            finally
            {
                cde.Signal();//建造完成一点后,通知监工
            }
        }
        /// <summary>
        /// 建房子第二道所需要的工序
        /// </summary>
        /// <param name="person"></param>
        static void BuilderHourseStep2(string person)
        {
            try
            {
                Console.WriteLine(string.Format("『{0}』BuilderHourseStep2.....", person));
            }
            finally
            {
                cde.Signal();
            }
        }
        /// <summary>
        /// 建房子第三道所需要的工序
        /// </summary>
        /// <param name="person"></param>
        static void BuilderHourseStep3(string person)
        {
            try
            {
                Console.WriteLine(string.Format("『{0}』BuilderHourseStep3.......", person));
            }
            finally
            {
                cde.Signal();
            }
        }
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

代码效果

 

2.超时演示,延续上面例子,建造房子存在延期的情况,譬如某个工作工作懒散,那么需要给整个一期建造设置一个总的时限。

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication18
{
    class Program
    {
        static CountdownEvent cde = null;
        static int overLimitTime = 5000;//第一期工作完成时间
        /// <summary>
        /// 模拟建房子
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            try
            {
                //建房子第一期工人
                string[] _bhPerson1 = new string[5] { "Yan", "Zhi", "wei", "Do", "Work" };
                //建房子第二期工人
                string[] _bhPerson2 = new string[3] { "Yan2", "Zhi2", "wei2" };
                //建房子第三期工人
                string[] _bhPerson3 = new string[3] { "Yan3", "Zhi3", "wei3" };

                using (cde = new CountdownEvent(Environment.ProcessorCount))//开始监管,相当于监工
                {
                    cde.Reset(_bhPerson1.Length);//设置第一期建造需要5个人
                    Random _workTime = new Random();
                    foreach (string person in _bhPerson1)
                    {
                        Task.Factory.StartNew(() =>
                        {
                            int _doWorkTime = _workTime.Next(3000, 6000);//工人工作时间
                            BuilderHourseStep1(person, _doWorkTime);
                        });

                    }
                    bool _bdStep1Result = cde.Wait(overLimitTime);
                    Console.WriteLine(string.Format("第一期建造状态:{0}.", _bdStep1Result == true ? "完成" : "延时"));//等待第一期建造完成 限制5秒内完成

                    Console.WriteLine("-----------------------");
                    if (!_bdStep1Result) return;
                    cde.Reset(_bhPerson2.Length);//设置第二期建需要三个人
                    foreach (string person in _bhPerson2)
                    {
                        Task.Factory.StartNew(() =>
                          {
                              BuilderHourseStep2(person);
                          });
                    }
                    cde.Wait();//等待第二期建造完成
                    Console.WriteLine("-----------------------");

                    cde.Reset(_bhPerson3.Length);//设置第三期建需要三个人
                    foreach (string person in _bhPerson3)
                    {
                        Task.Factory.StartNew(() =>
                            {
                                BuilderHourseStep3(person);
                            });
                    }
                    cde.Wait();//等待第三期建造完成
                    Console.WriteLine("-----------------------");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Excepton Message: {0}", ex.Message.Trim()));
            }
            finally
            {
                Console.ReadLine();
            }
        }

        /// <summary>
        /// 建房子第一道所需要的工序
        /// </summary>
        /// <param name="person"></param>
        static void BuilderHourseStep1(string person, int doWorkTime)
        {
            bool _isDoWork = doWorkTime < overLimitTime;
            try
            {
                if (_isDoWork)
                    Console.WriteLine(string.Format("『{0}』做事情花费了,{1}s........", person, doWorkTime));
                else
                    Console.WriteLine(string.Format("『{0}』做事情太墨迹,花费了{1}s....", person, doWorkTime));
                Thread.Sleep(doWorkTime);
            }
            finally
            {
                if (_isDoWork)
                    cde.Signal();//建造完成后,通知监工
            }
        }
        /// <summary>
        /// 建房子第二道所需要的工序
        /// </summary>
        /// <param name="person"></param>
        static void BuilderHourseStep2(string person)
        {
            try
            {
                Console.WriteLine(string.Format("『{0}』BuilderHourseStep2.....", person));
            }
            finally
            {
                cde.Signal();
            }
        }
        /// <summary>
        /// 建房子第三道所需要的工序
        /// </summary>
        /// <param name="person"></param>
        static void BuilderHourseStep3(string person)
        {
            try
            {
                Console.WriteLine(string.Format("『{0}』BuilderHourseStep3.......", person));
            }
            finally
            {
                cde.Signal();
            }
        }
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

代码效果

[C#]『CountdownEvent』任务并行库使用小计的更多相关文章

  1. [C#]『PLINQ』任务并行库使用小计

    并行 LINQ (PLINQ) 是 LINQ to Objects 的并行实现. PLINQ 实现完整的 LINQ 标准查询运算符集作为 T:System.Linq 命名空间的扩展方法,并具有用于并行 ...

  2. [C#]『Barrier』任务并行库使用小计

    Barrier  是一个对象,它可以在并行操作中的所有任务都达到相应的关卡之前,阻止各个任务继续执行. 如果并行操作是分阶段执行的,并且每一阶段要求各任务之间进行同步,则可以使用该对象. --MSDN ...

  3. [C#]『Task』任务并行库使用小计

    1.简单创建使用 using System; using System.Diagnostics; using System.Threading; using System.Threading.Task ...

  4. C# 任务并行库使用小计 z

    1.简单创建使用 using System; using System.Diagnostics; using System.Threading; using System.Threading.Task ...

  5. 『计算机视觉』imgaug图像增强库中部分API简介

    https://github.com/aleju/imgaug 介绍一下官方demo中用到的几个变换,工程README.md已经给出了API简介,个人觉得不好理解,特此单独记录一下: import n ...

  6. 『TensorFlow』分布式训练_其二_单机多GPU并行&GPU模式设定

    建议比对『MXNet』第七弹_多GPU并行程序设计 一.tensorflow GPU设置 GPU指定占用 gpu_options = tf.GPUOptions(per_process_gpu_mem ...

  7. 『开源』Slithice 2013 服务器集群 设计和源码

    相关介绍文章: <『设计』Slithice 分布式架构设计-支持一体式开发,分布式发布> <『集群』001 Slithice 服务器集群 概述> <『集群』002 Sli ...

  8. 『计算机视觉』Mask-RCNN_推断网络其四:FPN和ROIAlign的耦合

    一.模块概述 上节的最后,我们进行了如下操作获取了有限的proposal, # [IMAGES_PER_GPU, num_rois, (y1, x1, y2, x2)] # IMAGES_PER_GP ...

  9. 『计算机视觉』Mask-RCNN

    一.Mask-RCNN流程 Mask R-CNN是一个实例分割(Instance segmentation)算法,通过增加不同的分支,可以完成目标分类.目标检测.语义分割.实例分割.人体姿势识别等多种 ...

随机推荐

  1. 【Android - 进阶】之自定义视图浅析

    1       概述 Android自定义View / ViewGroup的步骤大致如下: 1) 自定义属性: 2) 选择和设置构造方法: 3) 重写onMeasure()方法: 4) 重写onDra ...

  2. 文件正在上传的转圈圈gif图片引出的fixed定位和absolute定位

     文件正在上传的转圈圈gif图片  一.文件上传时,未上传返回成功状态之前给个gif动态图片显示在页面,改善用户体验. <!--S 遮罩层 --> <div id="mas ...

  3. HP QC(Quality Center)在Windows 7 IE8 IE9下不能工作解决方案

    HP QC(Quantity Center)是一款不错的测试管理工具,公司的操作系统Windows 7登录到QC Server的Quality Center和Addin页面,客户端组件不能正常下载,从 ...

  4. linux中shell变量$#,$@,$0,$1,$2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中shell变量$#,$@,$0,$1,$2的含义解释:  变量说明:  $$  Shell本身的PID(ProcessID ...

  5. iotop,pt-ioprofile : mysql IO负载高的来源定位

    http://www.cnblogs.com/cenalulu/archive/2013/04/12/3016714.html 前言: 在一般运维工作中经常会遇到这么一个场景,服务器的IO负载很高(i ...

  6. insert into ... on duplicate key update 与 replace 区别

    on duplicate key update:针对主健与唯一健,当插入值中的主健值与表中的主健值,若相同的主健值,就更新on duplicate key update 后面的指定的字段值,若没有相同 ...

  7. 深入理解JavaScript系列+ 深入理解javascript之执行上下文

    http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html http://blog.csdn.net/hi_kevin/article/d ...

  8. C#泛型对类型参数的推断

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. GridView中使用如下button OnClientClick代码会出现解析错误

    在GridView中使用如下代码会出现解析错误: <asp:LinkButton ID="DeleteButton" runat="server" Cau ...

  10. How to fix “The program can’t start because MSVCR110.dll is missing from your computer.” error on Windows

    原文出处 How to fix “The program can’t start because MSVCR110.dll is missing from your computer.” error ...