本文主要介绍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. J - Max Sum

    J - Max Sum Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  2. EasyDSS视频点播服务器实现的多码率点播功能的说明

    EasyDSS流媒体音视频直播与点播服务器软件,是一套提供一站式的转码.点播.直播.检索.回放.录像下载服务的高性能RTMP/HLS/HTTP-FLV流媒体服务,极大地简化了流媒体相关业务的开发和集成 ...

  3. Apache Maven pom文件

    Welcome to Apache Maven Apache Maven is a software project management and comprehension tool. Based ...

  4. 【python】-- 模块、os、sys、time/datetime、random、logging、re

    模块 模块,用一堆代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个 ...

  5. 【python】-- json & pickle、xml、requests、hashlib、shelve、shutil、configparser、subprocess

    json & pickle Python中用于序列化的两个模块 json     用于[字符串]和 [python基本数据类型] 间进行转换 pickle   用于[python特有的类型] ...

  6. 【python】-- socketserver

    socketserver SocketServer服务端内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求.即:每个客户端请求连接到服务器时,Socket服务端 ...

  7. python+NLTK 自然语言学习处理四:获取文本语料和词汇资源

    在前面我们通过from nltk.book import *的方式获取了一些预定义的文本.本章将讨论各种文本语料库 1 古腾堡语料库 古腾堡是一个大型的电子图书在线网站,网址是http://www.g ...

  8. Bootstrap学习3--栅格系统

    备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html 目录1.简介2.栅格选项3.列偏移4.嵌套列5.列排序 ...

  9. Nvidia NVENC 硬编码预研总结

    本篇博客记录NVENC硬编码的预研过程 github:  https://github.com/MarkRepo/NvencEncoder 步骤如下: (1)环境搭建 (2)demo编译,测试,ARG ...

  10. HR_ROS 节点信息

    https://stackoverflow.com/questions/24638063/install-node-serialport-module-on-arm-linux https://blo ...