Parallel Programming-Parallel.Invoke
本文主要介绍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是最简单的并行编程模型,用于并行执行多个互不相干的方法。有几点需要注意
- 只有当所有的Action执行完成后Invoke才会返回(WaitAll)
- 在执行过程中有其中一个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的更多相关文章
- 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 ...
- Notes of Principles of Parallel Programming - TODO
0.1 TopicNotes of Lin C., Snyder L.. Principles of Parallel Programming. Beijing: China Machine Pres ...
- 4.3 Reduction代码(Heterogeneous Parallel Programming class lab)
首先添加上Heterogeneous Parallel Programming class 中 lab: Reduction的代码: myReduction.c // MP Reduction // ...
- Task Cancellation: Parallel Programming
http://beyondrelational.com/modules/2/blogs/79/posts/11524/task-cancellation-parallel-programming-ii ...
- Samples for Parallel Programming with the .NET Framework
The .NET Framework 4 includes significant advancements for developers writing parallel and concurren ...
- Parallel Programming for FPGAs 学习笔记(1)
Parallel Programming for FPGAs 学习笔记(1)
- Parallel Programming AND Asynchronous Programming
https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...
- 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 ...
- A Pattern Language for Parallel Programming
The pattern language is organized into four design spaces. Generally one starts at the top in the F ...
- 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 ...
随机推荐
- is assembler instruction and machine instuction atomic
1 assembler instruction depends,有的汇编指令会被assemble成多条机器指令. 2 机器指令 depends,有的机器指令也不是atomic的. 所以,不要希望在单条 ...
- Mybatis sql注入问题
预编译方式,即PreparedStatement,可以防注入:#{id} <select id="getBlogById" resultType="Blog&quo ...
- eclipse InvocationTargetException 错误解决
1.今天做一个推送的,用到了几个jar包,直接以User Library的形式加进 在单元测试中,测试的很好,没有任何问题, 但是在action中测试,一测试就崩. 跟踪以后出现InvocationT ...
- 在C语言中使用syslog打印日志到日志文件
参见 <unix 环境高级编程>第13 章 精灵进程 Syslog为每个事件赋予几个不同的优先级: LOG_EMERG——紧急情况 LOG_ALERT——应该被立即改正的问题,如系统数据库 ...
- C#自己写的第一个小程序,庆祝一下
Packages.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; na ...
- C#泛型命名潜规则
public class List<T>{} public class LinkedList<T>{} public class SortedList<TKey,TVal ...
- 海信电视 LED55K370 升级固件总结【含固件下载地址】
最早电视买回来,感觉垃圾软件太多,root后,删软件不小心删除了桌面,导致没桌面. 用ADB装了点软件,凑合可以用. 后来装了悟空遥控,然后装了沙发桌面,不影响使用了. 最近海信不停推送更新系统,改手 ...
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- Group By 和 Having, Where ,Order by执行顺序
1.Group By 和 Having, Where ,Order by这些关键字是按照如下顺序进行执行的:Where, Group By, Having, Order by. 首先where将最原始 ...
- hive启动时 Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
错误提示信息如下 [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleC ...