C#并行编程中的Parallel.Invoke

一、基础知识

并行编程:并行编程是指软件开发的代码,它能在同一时间执行多个计算任务,提高执行效率和性能一种编程方式,属于多线程编程范畴。所以我们在设计过程中一般会将很多任务划分成若干个互相独立子任务,这些任务不考虑互相的依赖和顺序。这样我们就可以使用很好的使用并行编程。但是我们都知道多核处理器的并行设计使用共享内存,如果没有考虑并发问题,就会有很多异常和达不到我们预期的效果。不过还好NET Framework4.0引入了Task Parallel Library(TPL)实现了基于任务设计而不用处理重复复杂的线程的并行开发框架。它支持数据并行,任务并行与流水线。核心主要是Task,但是一般简单的并行我们可以利用Parallel提供的静态类如下三个方法。

Parallel.Invoke  对给定任务实现并行开发

Parallel.For  对固定数目的任务提供循环迭代并行开发

parallel.Foreach 对固定数目的任务提供循环迭代并行开发

注意:所有的并行开发不是简单的以为只要将For或者Foreach换成Parallel.For与Parallel.Foreach这样简单。

PS:从简单的Invoke开始逐步深入探讨并行开发的主要知识点,也对自己学习过程中的积累做个总结,其中参考了博客园中的其他优秀博文

滴答的雨 异步编程:轻量级线程同步基元对象

首先感谢您,在我学习并行开发过程中,您的博文对我帮助很大。

二、Parallel.Invoke在并行中的使用

首先我们来看看它的两个重载方法:

  public static void Invoke(params Action[] actions);
  public static void Invoke(ParallelOptions parallelOptions, params Action[] actions);

Invoke主要接受params的委托actions,比如我们要同时执行三个任务,我们可以这样利用

  方式一
Parallel.Invoke(() => Task1(), () => Task2(), () => Task3());
方式二
Parallel.Invoke(Task1, Task2, Task3);
方式三
Parallel.Invoke(
                () =>
                {
                    Task1();
                },
               Task2,
                delegate () { Task3(); console.write('do someting!');});

这样Invoke就简单实现了Task1,Task2,Task3的并行开发。下面我们用实例来说明他们的执行规则。以及两个重载方法的使用。

三 、Demo

1、 Demo 1:

public class ParallelInvoke
{
/// <summary>
/// Invoke方式一 action
/// </summary>
public void Client1()
{
Stopwatch stopWatch = new Stopwatch(); Console.WriteLine("主线程:{0}线程ID : {1};开始", "Client1", Thread.CurrentThread.ManagedThreadId);
stopWatch.Start();
Parallel.Invoke(() => Task1("task1"), () => Task2("task2"), () => Task3("task3"));
stopWatch.Stop();
Console.WriteLine("主线程:{0}线程ID : {1};结束,共用时{2}ms", "Client1", Thread.CurrentThread.ManagedThreadId, stopWatch.ElapsedMilliseconds);
} private void Task1(string data)
{
Thread.Sleep(5000);
Console.WriteLine("任务名:{0}线程ID : {1}", data, Thread.CurrentThread.ManagedThreadId);
} private void Task2(string data)
{
Console.WriteLine("任务名:{0}线程ID : {1}", data, Thread.CurrentThread.ManagedThreadId);
} private void Task3(string data)
{
Console.WriteLine("任务名:{0}线程ID : {1}", data, Thread.CurrentThread.ManagedThreadId);
}

执行运行后结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY0AAAB2CAIAAAC751joAAALvklEQVR4nO2cW5bkOAhEc/+r8s5mPro7j0tAEGBZflTcL6csAUIQdrnnzOc/IYS4N5+rAxBCiATplBDi7kinhBB3RzolhLg70ikhxN35WLZtc0aJaX9GwHLS8rtR3tbgJk2ZfBXMcUb9Fi3f/rG/3nMg3iehvFVxd53mQTolCkXgVhJZVe9DeTtCafvSqbfReF5t3ivA9vOZb72419HIkS2klt0IpwO2eUbe3CSAzCxgrt9S2UyvMXE78ImCHiPrZm4NRcIEQjq7aVPFPC9vVaU7j+l+yc1G0rwvjOPBiOvBbRN1F1CEYYkdPFI6OIyzi5KXmDV5S0eWdel0v8xmh3F7MSsYcT3MKUZFU+rG46Rmb6JTYHBu3qRTHyNPrr5Lqh5PqfmZsz+vLBo6BUK1G/le21XpxnFfnZS3tJmX9ecUv4HC+Fhf9uJgPOJK+Ar4EA8xO2eww3jhI+9NSAt3M1KFp6Uu1uSN39fZDPsCPxsGo5FhfNlmxQXg3ouaJCqO7d/7SHQr8nIw1NQFHsHbYQKwnXl23txbDZ3q6Uhkx27hVJ1y8wnyL54KPkXcJFExPUKn3FLGMfOSsSBv7q0Ldarqt2oBH+6gU3ODEdfTPkVbGeTD7Ujd9HSkWvRV+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Ud5SGCWacjdN/qBE7RiOzxdCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghCrzmf70mhHgt0ikhxN2RTgkh7o50Sghxd6RTQgghhBBCCCGEEEIIIUTI/9adLG5H/hA8AAAAAElFTkSuQmCC" alt="" />

我们看到Invoke 执行Task三个方法主要有以下几个特点:

1、没有固定的顺序,每个Task可能是不同的线程去执行,也可能是相同的;

2、主线程必须等Invoke中的所有方法执行完成后返回才继续向下执行;这样对我们以后设计并行的时候,要考虑每个Task任务尽可能差不多,如果相差很大,比如一个时间非常长,其他都比较短,这样一个线程可能会影响整个任务的性能。这点非常重要

3、这个非常简单就实现了并行,不用我们考虑线程问题。主要Framework已经为我们控制好线程池的问题。

ps:如果其中有一个异常怎么办? 带做这个问题修改了增加了一个Task4.

2、 Demo2

    public class ParallelInvoke
{
/// <summary>
/// Invoke方式一 action
/// </summary>
public void Client1()
{
Stopwatch stopWatch = new Stopwatch(); Console.WriteLine("主线程:{0}线程ID : {1};开始", "Client1", Thread.CurrentThread.ManagedThreadId);
stopWatch.Start(); try
{
Parallel.Invoke(() => Task1("task1"), () => Task2("task2"), () => Task3("task3"), delegate () { throw new Exception("我这里发送了异常"); });
}
catch (AggregateException ae)
{
foreach (var ex in ae.InnerExceptions)
Console.WriteLine(ex.Message);
} stopWatch.Stop();
Console.WriteLine("主线程:{0}线程ID : {1};结束,共用时{2}ms", "Client1", Thread.CurrentThread.ManagedThreadId, stopWatch.ElapsedMilliseconds);
}

主要看 delegate() { throw new Exception("我这里发送了异常");} 增加了这个委托Task3. 然后我们看结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeYAAACTCAIAAADUaS1eAAAN0UlEQVR4nO2dUbbjKg5FPYyeb0/Mk6jVXzWW7o/ql3KQdBAYcMjd+6OWg0ESMhwcO3U5fv/+/evXr3/9+z//BQCAz+ZAsgEAdgHJBgDYBiQbAGAb/kr2YTjP0xZmqv0pEc2Tlr8b8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AtgHJBgDYBiQbAGAbkGwAgG1AsgEAtgHJBgDYBiQbAGAbkGwAgG1AsgEAtgHJBgDYBiQbAGAbkGwAgG1AsgEAtqG6xcHTAQIAwD8g2QAA24BkAwBsA5INALANSDYAwDbwixEAgG1AsgEAtgHJBgDYBiQbAGAbkGwAgG1AsgEAtgHJBgDYBiQbAGAbkGwAgG1AsgEAtgHJBgDYBiQbAGAbkGwAgG1AsgEAtgHJBgDYBiQbAGAbkGwAgF34H9sWWPmN9Mp6AAAAAElFTkSuQmCC" alt="" />

这里我们发现即使有异常程序也会完成执行,而且不会影响其他Task的执行。

3、demo3 重载方法ParallelOptions 的使用。

理解ParallelOptions建议大家异步编程:轻量级线程同步基元对象讲的非常详细。

主要理解两个参数:

CancellationToken     控制线程的取消
     MaxDegreeOfParallelism  设置最大的线程数,有时候可能会跑遍所有的内核,为了提高其他应用程序的稳定性,就要限制参与的内核

下面从代码上看效果如何?

 public class ParallelInvoke
{
    // 定义CancellationTokenSource 控制取消
readonly CancellationTokenSource _cts = new CancellationTokenSource(); /// <summary>
/// Invoke方式一 action
/// </summary>
public void Client1()
{
Console.WriteLine("主线程:{0}线程ID : {1};开始{2}", "Client3", Thread.CurrentThread.ManagedThreadId, DateTime.Now); var po = new ParallelOptions
{
CancellationToken = _cts.Token, // 控制线程取消
MaxDegreeOfParallelism = 3 // 设置最大的线程数3,仔细观察线程ID变化
}; Parallel.Invoke(po, () => Task1("task1"), ()=>Task5(po), Task6); Console.WriteLine("主线程:{0}线程ID : {1};结束{2}", "Client3", Thread.CurrentThread.ManagedThreadId, DateTime.Now);
} private void Task1(string data)
{
Thread.Sleep(5000);
Console.WriteLine("任务名:{0}线程ID : {1}", data, Thread.CurrentThread.ManagedThreadId);
}

// 打印数字
private void Task5(ParallelOptions po)
{
Console.WriteLine("进入Task5线程ID : {0}", Thread.CurrentThread.ManagedThreadId);
int i = 0;
while (i < 100)
{
// 判断是否已经取消
if (po.CancellationToken.IsCancellationRequested)
{
Console.WriteLine("已经被取消。");
return;
} Thread.Sleep(100);
Console.Write(i + " ");
Interlocked.Increment(ref i);
} } /// <summary>
/// 10秒后取消
/// </summary>
private void Task6()
{
Console.WriteLine("进入取消任务,Task6线程ID : {0}", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000 * 10);
_cts.Cancel();
Console.WriteLine("发起取消请求...........");
}

执行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8wAAAC1CAIAAAAFj+GtAAAeuElEQVR4nO2dQXIjOZJFeYw5Xt5lLsYLzLKtV7WqTS37BjmLtJaFAu6O7w4HSEnvLcooBPDx4QAZP1kU9fjnn3/+/vvvv/7661//+tf//O+/fwMAAAAAwBoPQjYAAAAAQC+EbAAAAACAZgjZAAAAAADNELIBAAAAAJohZAMAAAAANEPIBgAAAABohpANAAAAANAMIRsAAAAAoJl7yH4MPJ/PsVHp9qclGC4qfw+ozxnMolFJAAAA2Mfv37//MzAP2Q8to3gh0hv+/C/Xx1fUZX0RqE8Wc9XTOhCyAQAA4DD1kD1FTzZmPBKj0teF+qyQWv7bhuwuD++wFgAAALiihuzCO4hP603Z5+d3YW9utoZIxerYf2XGqZ9x9q76mIsNKnCA3nlTxyPbOai/N3A8UdNqi43TLQum1v17FPyYIt5iU36CRb3kSAMAAATs+rjI0w+OYhgy76bhWiLGIPLxoCsBlP3sq0/ccjKUtM8rLvbG2Nk7GOOPwZBx15Q1FnS8g6r09/wH9mp+PHveWN3P2L/gBwAA4AxbQraXbLxRZhgaG1fuoOtZYQUvTu2uz7TlWChpn1dZ7K19fDD+6F2a+i8s0NvrrGyLf4/FJ47op2zjVecZAABgymt+8TEVMdfJRoFjBnbXh5D9GALZ0yH2OfW/mD5FHX3Vt6vl+qf8TE+s52ectKZz7DwDAABM2Riyrx3MQDP233SPLCSewKqZzJ6fA5zZOQgEm+pjrivusImWeZ8ZxrnGB1M/3hClsNPdTLWL83qXyvVP+YkP+WOop1ciXWfUBAAAeBMSIVuPNQ8tQ3i5JJ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程中线程数码不超过3个。

2、CancellationTokenSource/CancellationToken控制任务的取消。

四、总结

Parallel.Invoke 的使用过程中我们要注意以下特点:

1、没有特定的顺序,Invoke中的方法全部执行完才返回,但是即使有异常在执行过程中也同样会完成,他只是一个很简单的并行处理方法,特点就是简单,不需要我们考虑线程的问题。

2、如果在设计Invoke中有个需要很长时间,这样会影响整个Invoke的效率和性能,这个我们在设计每个task时候必须去考虑的。

3、Invoke 参数是委托方法。

4、当然Invoke在每次调用都有开销的,不一定并行一定比串行好,要根据实际情况,内核环境多次测试调优才可以。

5、异常处理比较复杂。

C#并行编程的更多相关文章

  1. C#并行编程系列-文章导航

    菜鸟初步学习,不对的地方请大神指教,参考<C#并行编程高级教程.pdf> 目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C# ...

  2. Parallel并行编程初步

    Parallel并行编程可以让我们使用极致的使用CPU.并行编程与多线程编程不同,多线程编程无论怎样开启线程,也是在同一个CPU上切换时间片.而并行编程则是多CPU核心同时工作.耗时的CPU计算操作选 ...

  3. .Net中的并行编程-2.ConcurrentStack的实现与分析

    在上篇文章<.net中的并行编程-1.基础知识>中列出了在.net进行多核或并行编程中需要的基础知识,今天就来分析在基础知识树中一个比较简单常用的并发数据结构--.net类库中无锁栈的实现 ...

  4. .Net中的并行编程-3.ConcurrentQueue实现与分析

    在上文<.Net中的并行编程-2.ConcurrentQueue的实现与分析> 中解释了无锁的相关概念,无独有偶BCL提供的ConcurrentQueue也是基于原子操作实现, 由于Con ...

  5. C#~异步编程再续~大叔所理解的并行编程(Task&Parallel)

    返回目录 并行这个概念出自.net4.5,它被封装在System.Threading.Tasks命名空间里,主要提供一些线程,异步的方法,或者说它是对之前Thread进行的二次封装,为的是让开发人员更 ...

  6. .NET并行编程实践(一:.NET并行计算基本介绍、并行循环使用模式)

    阅读目录: 1.开篇介绍 2.NET并行计算基本介绍 3.并行循环使用模式 3.1并行For循环 3.2并行ForEach循环 3.3并行LINQ(PLINQ) 1]开篇介绍 最近这几天在捣鼓并行计算 ...

  7. C#并行编程之数据并行

    所谓的数据并行的条件是: 1.拥有大量的数据. 2.对数据的逻辑操作都是一致的. 3.数据之间没有顺序依赖. 运行并行编程可以充分的利用现在多核计算机的优势.记录代码如下: public class ...

  8. OpenMP共享内存并行编程详解

    实验平台:win7, VS2010 1. 介绍 平行计算机可以简单分为共享内存和分布式内存,共享内存就是多个核心共享一个内存,目前的PC就是这类(不管是只有一个多核CPU还是可以插多个CPU,它们都有 ...

  9. .NET并行编程1 - 并行模式

    设计模式——.net并行编程,清华大学出版的中译本. 相关资源地址主页面: http://parallelpatterns.codeplex.com/ 代码下载: http://parallelpat ...

  10. 【读书笔记】.Net并行编程(三)---并行集合

    为了让共享的数组,集合能够被多线程更新,我们现在(.net4.0之后)可以使用并发集合来实现这个功能.而System.Collections和System.Collections.Generic命名空 ...

随机推荐

  1. Kendo UI开发教程(14): Kendo MVVM 数据绑定(三) Click

    Click绑定可以把由ViewModel定义的方法不绑定到目标DOM的click事件.当点击目标DOM元素时触发ViewModel的对应方法.例如: 使用Click绑定 1 <div id=&q ...

  2. Embedded Linux Primer----嵌入式Linux基础教程--前言

    APractical,Real-WorldApproach ChristopherHallinan 计算机无处不在. 事实上,当然了,对于那些大约在过去25年期间没有生活在洞穴中的人来说没有什么惊奇的 ...

  3. 使用xml和java代码混合控制UI界面

    main.xml.................... <?xml version="1.0" encoding="utf-8"?> <Li ...

  4. Wix打包系列 (六)制作升级和补丁包

    原文:Wix打包系列 (六)制作升级和补丁包 前面我们已经知道怎么制作一个完整安装包了,但我们的软件往往不能一次性就满足客户的需要,当客户需要我们给软件进行升级的时候,我们应该怎么做呢? 在这之前,我 ...

  5. 金融界高富帥現身快男北京唱區 陳樂:我拿生活養夢想__娛樂新聞_Yes娛樂

    http://m.baidu.com/tc?pn=15&bd_page_type=1&pu=sz%401320%5F1001%2Cta%40iphone%5F2%5F4%2E1%5F3 ...

  6. IntelliJ IDEA 14 注册码生成java代码(转)

    https://confluence.jetbrains.com/display/IntelliJIDEA/Previous+IntelliJ+IDEA+Releases 分享几个license: ( ...

  7. SVN基于Maven的Web项目更新,本地过程详细解释

    周围环境 MyEclipse:10.7 Maven:3.1.1 概要 最近在做项目,MyEclipse下载SVN基于上述Maven的Web问题,有时候搞了非常半天,Maven项目还是出现叉号,最后总结 ...

  8. Java集合关于ArrayList

    ArrayList实现源码分析 2016-04-11 17:52 by 淮左, 207 阅读, 0 评论, 收藏, 编辑 本文将以以下几个问题来探讨ArrayList的源码实现1.ArrayList的 ...

  9. ecshop后台权限增加

    1.在后台“推荐管理”里添加“推荐人分成”.“会员分成”两个操作功能以及权限     index.php?act=menu     incluedes/inc_priv.php:权限对照表.inc_m ...

  10. pygame系列_游戏中的事件

    先看一下我做的demo: 当玩家按下键盘上的:上,下,左,右键的时候,后台会打印出玩家所按键的数字值,而图形会随之移动 这是客观上面存在的现象. 那么啥是事件呢? 你叫我做出定义,我不知道,我只能举个 ...