Parallel Programming-使用CancellationTokenSource调度并行运行的Task
本文主要介绍使用CancellationTokenSource调度并行运行的Task。
一、使用场景
有多个Task并行运行时,如果其中一个Task所运行的程序出现异常,我们想马上终止所有待执行的Task。这样对系统的性能等各个方面都是有好处的。
二、源码
2.1 被多线程执行的代码
public class Handler
{
public void DoSomething(CancellationTokenSource cts, int index)
{
if (cts.IsCancellationRequested)
{
return;
}
if (index == )
{
cts.Cancel();
}
Console.WriteLine(index);
}
}
两点:
- 判断cts是否已经取消,如果取消则不再执行。
- index的=2的时候取消cts。这样其他在此之后的Task再执行进来的时候IsCancellationRequested就是true。不会再执行。
2.2 Task调度代码
public class AppClient
{
public static void Main()
{
var cts = new CancellationTokenSource();
var childTasks = new List<Task>();
var parentTask = new Task(() =>
{
var taskFactory = new TaskFactory(cts.Token, TaskCreationOptions.AttachedToParent,
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
for (var i = ; i < ; i++)
{
var tempIndex = i;
var childTask = taskFactory.StartNew(() => new Handler().DoSomething(cts, tempIndex));
childTasks.Add(childTask);
}
foreach (var task in childTasks)
{
task.ContinueWith(t => cts.Cancel(), TaskContinuationOptions.OnlyOnFaulted);
}
});
parentTask.Start();
parentTask.Wait();
Console.Read();
}
}
上面的代码在正常情况下会有100个Task产生,会Attach到父Task上,由父Task统一调度(parentTask.Start; parentTask.Wait)
2.3 Task.ContinueWith
foreach (var task in childTasks)
{
task.ContinueWith(t => cts.Cancel(), TaskContinuationOptions.OnlyOnFaulted);
}
这块代码的主要含义是当task发生错误,比如抛出异常的时候,对cts发起取消。这样cts处于取消状态后(IsCancellationRequested==true),后续的操作都会直接return,不再执行。
这就回到了本文开始的话题:有多个Task并行运行时,如果其中一个Task所运行的程序出现异常,我们想马上终止所有待执行的Task。这样对系统的性能等各个方面都是有好处的。
并行编程打算写一个系列的文章好好总结一下。
Parallel Programming-使用CancellationTokenSource调度并行运行的Task的更多相关文章
- 用Parallel.For()和Parallel.For<TLocal>()方法实现并行运行迭代
Parallel类是.NET 4中新增的抽象线程类.如果你开发用的是VS2008或更低版本,那么就直接关闭吧,下面两个示例用了匿名委托,如果不知道匿名委托的语法,那么先去简单了解一下,不然很难理解示例 ...
- Task Cancellation: Parallel Programming
http://beyondrelational.com/modules/2/blogs/79/posts/11524/task-cancellation-parallel-programming-ii ...
- 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 // ...
- Samples for Parallel Programming with the .NET Framework
The .NET Framework 4 includes significant advancements for developers writing parallel and concurren ...
- kubernetes调度pod运行于master节点上
应用背景: 使用kubeadm部署的kubernetes集群,其master节点默认拒绝将pod调度运行于其上的,加点官方的术语就是:master默认被赋予了一个或者多个“污点(taints)”,“污 ...
- Parallel Programming for FPGAs 学习笔记(1)
Parallel Programming for FPGAs 学习笔记(1)
- 使用Pabot并行运行RF案例
一.问题引入 在做接口自动化时随着案例增多,特别是流程类案例增多,特别是asp.net的webform类型的项目,再加上数据库校验也比较耗时,导致RF执行案例时间越来越长,就遇到这样一个问题,705个 ...
- Parallel Programming AND Asynchronous Programming
https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...
随机推荐
- Problem_A
Problem_A Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
- 大数据学习系列(1)-- linux之文件系统结构介绍
1./ 根目录 --------- 所有目录挂在其下 2./boot --------- 存放Ubuntu内核和系统启动文件.系统启动时这些文件先被装载. 3./etc --------- 系统的配置 ...
- org.apache.poi3.1.7 Excle并发批量导入导出
org.apache.poi3.1.7 升级,需要修改设置方式: 1.org.apache.poi3.1.4 的设置单元格: XSSFCellStyle cellStyle = wb.createCe ...
- php建立一个空类: stdClass
$pick = new stdClass; $pick->type = 'full'; ;
- Python菜鸟之路:Python基础
一.Python版本升级至3.0的必然性 In November 2014, it was announced that Python 2.7 would be supported until 202 ...
- 在Nginx/Openresty中启用http2支持
转自个人博客 chinazt.cc 以下摘自http2的介绍: HTTP/2 源自 SPDY/2 SPDY 系列协议由谷歌开发,于 2009 年公开.它的设计目标是降低 50% 的页面加载时间.当下很 ...
- Android Interactive Animation
Ref:收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation Ref:界面特效 Ref:BaseAnimation是基于开源的APP,致力于收集各种动画效果( ...
- Windows下重置MySQL密码(最开始是因为Access denied for user 'root'@'localhost'这个原因,无法登陆 'root'@'localhost')
本人使用的MySQL5.5,其他版本未测试过. 方法一: 更改密码: mysql -u root -p Enter password:*** mysql>use mysql; 选择数据库 Dat ...
- rails member collection
resources :theses do resources :document do get :download, :on => :member end end <a class=&qu ...
- c#命名规则参考
命名规则参考:1.从组件类型名中移去T前缀.例如TButton变成Button.2.除了第一个元音,删去所有元音字母.例如,Button变成bttn,Edit变成edt.3.压缩双字母.例如,bttn ...