【C# Parallel】ParallelLoopState
总结
总之,要编写一个健壮的并行循环,必须在并行循环体中检测 ParallelLoopState 对象的 IsExceptional, IsStopped 和 LowestBreakIteration 三个属性,出于简化编程的目的, ParallelLoopState 提供了一个 ShouldExitCurrentIteration 属性,当Stop()\cancel()时,ShouldExitCurrentIteration等于true。
| 方法 | 属性 | |
| 异常 | IsExceptional | ShouldExitCurrentIteration |
| Stop | IsStopped | ShouldExitCurrentIteration |
| Break | LowestBreakIteration(循环索引起始是0) |
stop
可以多次被调用,对应的属性IsExceptional、IsStopped。当发生break后,IsStopped等于true,ShouldExitCurrentIteration等于true。

ParallelLoopResult plr= Parallel.For(1,20,(index, LoopState) =>{
if (i == 10)
{
// 当某一个循环单元的数==10,
// 则停止所有线程的Parallel.For的执行,已经执行循环执行完当前一轮循环后停止,未执行的循环直接停止执行。
LoopState.Stop();//执行Stop()LowestBreakIteration为空,Stop/Break互斥两个只能有一个执行,Stop停止循环比Break快。
Console.WriteLine("ShouldExitCurrentIteration " + LoopState.ShouldExitCurrentIteration);//break 执行后ShouldExitCurrentIteration为true
return;//不加return,可能会发生该进程资源未释放。
}
Console.WriteLine(index+ " ThreadId" + Environment.CurrentManagedThreadId);
});
Console.WriteLine("LowestBreakIteration:" + plr.LowestBreakIteration);
break
可以多次被调用。对应的属性LowestBreakIteration、ShouldExitCurrentIteration。当发生break后,LowestBreakIteration等于最小那个index,ShouldExitCurrentIteration等于false
由于多线程执行,所以会有多个线程同时发生break,LowestBreakIteration取for循环最小那个index 。

ParallelLoopResult plr= Parallel.For(1,20,(index, LoopState) =>{
if (index == 10)
{
// 当某一个循环单元的数==10,
// 则首先停止当前 线程的Parallel.For的执行
//其他线程的For 满足条件则继续执行,直到满足条件或完成
// 所有执行单元结束后退出Parallel.For的执行
LoopState.Break();//LowestBreakIteration有值,Stop/Break互斥两个只能有一个执行,Stop停止循环比Break快。
Console.WriteLine("ShouldExitCurrentIteration " + LoopState.ShouldExitCurrentIteration);//break 执行后对ShouldExitCurrentIteration为false 没影响
return;//不加return,可能会发生该进程资源未释放。
}
Console.WriteLine(index+ " ThreadId" + Environment.CurrentManagedThreadId);
});
Console.WriteLine("LowestBreakIteration:" + plr.LowestBreakIteration);
CancellationTokenSource 取消并行
namespace CancelParallelLoops
{
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks; class Program
{
static void Main()
{
int[] nums = Enumerable.Range(0, 10000000).ToArray();
CancellationTokenSource cts = new CancellationTokenSource(); // Use ParallelOptions instance to store the CancellationToken
ParallelOptions po = new ParallelOptions();
po.CancellationToken = cts.Token;
po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
Console.WriteLine("Press any key to start. Press 'c' to cancel.");
Console.ReadKey(); // Run a task so that we can cancel from another thread.
Task.Factory.StartNew(() =>
{
if (Console.ReadKey().KeyChar == 'c')
cts.Cancel();
Console.WriteLine("press any key to exit");
}); try
{
Parallel.ForEach(nums, po, (num) =>
{
double d = Math.Sqrt(num);
Console.WriteLine("{0} on {1}", d, Thread.CurrentThread.ManagedThreadId);
});
}
catch (OperationCanceledException e)
{
Console.WriteLine(e.Message);
}
finally
{
cts.Dispose();
} Console.ReadKey();
}
}
}
IsExceptional 属性(不知道如何使用)
ParallelLoopState 的 IsExceptional 属性为 true ,ShouldExitCurrentIteration等于true。
ShouldExitCurrentIteration 属性
总之,要编写一个健壮的并行循环,必须在并行循环体中检测 ParallelLoopState 对象的 IsExceptional, IsStopped 和 LowestBreakIteration 三个属性,出于简化编程的目的, ParallelLoopState 提供了一个 ShouldExitCurrentIteration 属性,当Stop()\cancel()时,ShouldExitCurrentIteration等于true。
Stop和Break
可以分别用来控制Parallel.For的执行。
Stop表示Parallel.For的执行立刻停止,无论其他执行单元是否达到停止的条件。
Break表示满足条件的当前执行单元立刻停止,而对于其他执行单元,其中满足停止条件也会通过Break停止,其他未满足停止条件的则会继续执行下去,从而全部执行完毕,自然停止。当所有执行单元停止后,Parallel.For函数才停止执行并退出。
执行Stop()LowestBreakIteration为空, LoopState.Break();LowestBreakIteration有值,Stop/Break互斥两个只能有一个执行,Stop停止循环比Break快。
break类似于for的continue,而stop就类似于for的break。
【C# Parallel】ParallelLoopState的更多相关文章
- 【C# Parallel】开端
使用条件 1.必须熟练掌握锁.死锁.task的知识,他是建立这两个的基础上的.task建立在线程和线程池上的. 2.并不是所有代码都适合并行化. 例如,如果某个循环在每次迭代时只执行少量工作,或它在很 ...
- 【C# Parallel】ParallelOptions
ParallelOptions 构造函数 此构造函数用默认值初始化实例. MaxDegreeOfParallelism 初始化为-1,表示没有对应采用的并行度进行上限设置. CancellationT ...
- 【知识点整理】Oracle中NOLOGGING、APPEND、ARCHIVE和PARALLEL下,REDO、UNDO和执行速度的比较
[知识点整理]Oracle中NOLOGGING.APPEND.ARCHIVE和PARALLEL下,REDO.UNDO和执行速度的比较 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 ...
- 【静默安装】configToolAllCommands响应文件问题
[静默安装]configToolAllCommands响应文件问题 客户在静默安装RAC 12.1.0.2的时候有如下的输出: Successfully Setup Software. As inst ...
- 【Unity Shaders】学习笔记——渲染管线
[Unity Shaders]学习笔记——Shader和渲染管线 转载请注明出处:http://www.cnblogs.com/-867259206/p/5595924.html 写作本系列文章时使用 ...
- 【exp/imp】将US7ASCII字符集的dmp文件导入到ZHS16GBK字符集的数据库中
[exp/imp]将US7ASCII字符集的dmp文件导入到ZHS16GBK字符集的数据库中 1.1 BLOG文档结构图 1.2 前言部分 1.2.1 导读和注意事项 各位技术爱好者,看完本文后 ...
- 【新特性】JDK1.8
一.简介 毫无疑问,Java 8是Java自Java 5(发布于2004年)之后的最重要的版本.这个版本包含语言.编译器.库.工具和JVM等方面的十多个新特性.在本文中我们将学习这些新特性,并用实际的 ...
- 【OCP|OCM】Oracle培训考证系列
[OCP|OCM]Oracle培训考证系列 我的个人信息 网名:小麦苗 QQ:646634621 QQ群:618766405 我的博客:http://blog.itpub.net/26736162 ...
- 【大数据】Scala学习笔记
第 1 章 scala的概述1 1.1 学习sdala的原因 1 1.2 Scala语言诞生小故事 1 1.3 Scala 和 Java 以及 jvm 的关系分析图 2 1.4 Scala语言的特点 ...
随机推荐
- 根据happens-before法则借助同步
在文章的开始,我们先来看一段代码以及他的执行情况: public class PossibleRecording{ static int x = 0, y = 0; static int a = 0, ...
- Servlet监听器统计网站在线人数
本节我们利用 Servlet 监听器接口,完成一个统计网站在线人数的案例.当一个用户登录后,显示欢迎信息,同时显示出当前在线人数和用户名单.当用户退出登录或 Session 过期时,从在线用户名单中删 ...
- gin框架中使用jwt
生成解析token 如今有很多将身份验证内置到API中的方法 -JSON Web令牌只是其中之一.JSON Web令牌(JWT)作为令牌系统而不是在每次请求时都发送用户名和密码,因此比其他方法(如基本 ...
- golang中goroutine
1. 概念 goroutine 奉行通过通信来共享内存,而不是共享内存来通信 goroutine 是由go的运行时(runtime)调度和管理的 go程序会智能的将goroutine中的任务合理的分配 ...
- 查看Linux系统信息
1.查看内核 [root@localhost etc]# uname -aLinux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue No ...
- 3D建模服务提供更高效、专业的3D制作能力,“筑”力开发者
3D建模服务(3D Modeling Kit)是HMS Core在图形图像领域又一技术开放.3D建模产品的定位就是要做快速.简洁.低成本的3D制作能力,并陆续开放给有3D模型.动画游戏制作等能力诉求的 ...
- APC 篇—— APC 执行
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...
- AT5760 Manga Market
首先一个想法就是可以考虑令 \(dp_{i, j}\) 表示当前考虑到了第 \(i\) 个商店,当前到了时刻 \(j\) 能走过最多的商店数量.但是你会发现这个 \(dp\) 转移的顺序并不是简单的从 ...
- Android利用zxing生成二维码
感谢大佬:https://blog.csdn.net/mountain_hua/article/details/80646089 **gayhub上的zxing可用于生成二维码,识别二维码 gayhu ...
- 7.3php编译安装最终版
进过无数次折腾后发现编译php安装的有些难, 问题可能是自己对linux不太了解吧. [root@third src]# cd php-7.3.6 [root@third php-7.3.6]# ./ ...