本文主要介绍Parallel.Invoke的使用。

一、使用例子

 class ParallelInvoke
{
public void Action1()
{
Thread.Sleep();
Console.WriteLine("in action1");
} public void Action2()
{
Thread.Sleep();
Console.WriteLine("in action2");
} public void ParallelAction()
{
Parallel.Invoke(() => Action1(), () => Action2());
}
}
 class Program
{
static void Main(string[] args)
{
var stopwatch = Stopwatch.StartNew();
stopwatch.Start();
new ParallelInvoke().ParallelAction();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
Console.Read();
}
}

二、 运行截图

上面的action1和action2如果并行执行至少需要3000*2毫秒,但是使用Invoke,内部并行执行,时间减半。

三、 分析

Parallel.Invoke是最简单的并行编程模型,用于并行执行多个互不相干的方法。有几点需要注意

  1. 只有当所有的Action执行完成后Invoke才会返回(WaitAll)
  2. 在执行过程中有其中一个Action发生异常,Invoke不会马上抛出异常,而是等所有的Action完成以后再次抛出异常

四、one more thing

4.1 MaxDegreeOfParallelism

Parallel.Invoke可以通过指定ParallelOption指定最大并行数量。

    public void ParallelAction()
{
Parallel.Invoke
(
new ParallelOptions()
{
MaxDegreeOfParallelism = ,
},
() => Action1(),
() => Action2()
);
}

上面的代码设置成了1,其实就是串行执行了。

执行时间如下。

4.2 CancellationSourceToken

同样可以通过ParallelOption指定CancellationSourceToken,多个并行任务之间可以协调取消

   class ParallelInvoke
{
public void Action1(CancellationTokenSource cts)
{
cts.Cancel();
Thread.Sleep();
Console.WriteLine("in action1");
} public void Action2(CancellationTokenSource cts)
{
if (cts.IsCancellationRequested)
{
return;
}
Thread.Sleep();
Console.WriteLine("in action2");
} public void ParallelAction()
{
CancellationTokenSource cts = new CancellationTokenSource(); Parallel.Invoke
(
new ParallelOptions()
{
CancellationToken = cts.Token,
},
() => Action1(cts),
() => Action2(cts)
);
}

Parallel Programming-Parallel.Invoke的更多相关文章

  1. Fork and Join: Java Can Excel at Painless Parallel Programming Too!---转

    原文地址:http://www.oracle.com/technetwork/articles/java/fork-join-422606.html Multicore processors are ...

  2. Notes of Principles of Parallel Programming - TODO

    0.1 TopicNotes of Lin C., Snyder L.. Principles of Parallel Programming. Beijing: China Machine Pres ...

  3. 4.3 Reduction代码(Heterogeneous Parallel Programming class lab)

    首先添加上Heterogeneous Parallel Programming class 中 lab: Reduction的代码: myReduction.c // MP Reduction // ...

  4. Task Cancellation: Parallel Programming

    http://beyondrelational.com/modules/2/blogs/79/posts/11524/task-cancellation-parallel-programming-ii ...

  5. Samples for Parallel Programming with the .NET Framework

    The .NET Framework 4 includes significant advancements for developers writing parallel and concurren ...

  6. Parallel Programming for FPGAs 学习笔记(1)

    Parallel Programming for FPGAs 学习笔记(1)

  7. Parallel Programming AND Asynchronous Programming

    https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...

  8. Introduction to Multi-Threaded, Multi-Core and Parallel Programming concepts

    https://katyscode.wordpress.com/2013/05/17/introduction-to-multi-threaded-multi-core-and-parallel-pr ...

  9. A Pattern Language for Parallel Programming

    The pattern language is organized into four design spaces.  Generally one starts at the top in the F ...

  10. parallel programming. this causual litery nots represents my recent progress in parallel programming in c#.It`s interesting.

    not to say extra words,let`s start the code. pasted below: using System; using System.Collections.Ge ...

随机推荐

  1. MFC添加菜单资源与菜单执行函数的两种命令形式

    添加资源->新建一个菜单资源->选择相应的对话框 菜单的执行函数命令形式: COMMAD 是指点击菜单后的执行命令 UPDATE_COMMAND_UI 是指点击菜单后菜单状态的函数

  2. SpringMVC结合REST

    一.什么是REST REST即 Representational State Transfer,也就是(资源)表现层状态转化.资源是指网络上的一个实体或者说是网络上的一个具体信息. 每种资源对应一个特 ...

  3. On the importance of initialization and momentum in deep learning

    Ilya Sutskever1 ilyasu@google.com James Martens jmartens@cs.toronto.edu George Dahl gdahl@cs.toronto ...

  4. You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

    [root@localhost dreamstart]# composer installLoading composer repositories with package informationI ...

  5. scala如何解决类型强转问题

    scala如何解决类型强转问题 scala属于强类型语言,在指定变量类型时必须确定数据类型,即便scala拥有引以为傲的隐式推到,这某些场合也有些有心无力. 例如: java同属强类型语言,但java ...

  6. python3保存一个网页

    import requests res = requests.get("http://www.baidu.com") savefile = open("baidu.htm ...

  7. 《程序员代码面试指南》第二章 链表问题 将单链表每K个节点之间逆序

    样例 链表1-2-3-4-5-6-7-8-9-10 K=3 ,结果 3-2-1-6-5-4-9-8-7-10 java代码 /** * @Description:将单链表每K个节点之间逆序 * @Au ...

  8. [原创]java WEB学习笔记21:MVC案例完整实践(part 2)---DAO层设计

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. python实现并绘制 sigmoid函数,tanh函数,ReLU函数,PReLU函数

    Python绘制正余弦函数图像 # -*- coding:utf-8 -*- from matplotlib import pyplot as plt import numpy as np impor ...

  10. nginx gzip 压缩设置

    mime.types 中包含所有文件的类型,不知道的可以去里面查询 gzip配置的常用参数 gzip on|off;  #是否开启gzip gzip_buffers 32 4K| 16 8K #缓冲( ...