Parallel.For循环 和 Parallel.ForEach循环
大家好,又见面了,我是你们的朋友全栈君。
大多时候,我们的循环结构的每一次迭代 依赖于上一次迭代的计算或行为。
但是,有的时候又不是这样。如果迭代之间彼此独立,并且程序运行在多核处理器的机器上,如果能将不同的迭代放在不同的处理器上并行处理的话,将会受益匪浅。Parallel.For 和 Parallel.ForEach结构就是这样做的。
一、Parallel.For
1、Parallel.For方法有12个重载:
public static ParallelLoopResult For(int fromInclusive, int toExclusive, Action<int> body);
public static ParallelLoopResult For(long fromInclusive, long toExclusive, Action<long> body);
public static ParallelLoopResult For(int fromInclusive, int toExclusive, Action<int, ParallelLoopState> body);
public static ParallelLoopResult For(long fromInclusive, long toExclusive, Action<long, ParallelLoopState> body);
public static ParallelLoopResult For(int fromInclusive, int toExclusive, ParallelOptions parallelOptions, Action<int> body);
public static ParallelLoopResult For(long fromInclusive, long toExclusive, ParallelOptions parallelOptions, Action<long> body);
public static ParallelLoopResult For(int fromInclusive, int toExclusive, ParallelOptions parallelOptions, Action<int, ParallelLoopState> body);
public static ParallelLoopResult For(long fromInclusive, long toExclusive, ParallelOptions parallelOptions, Action<long, ParallelLoopState> body);
public static ParallelLoopResult For<TLocal>(int fromInclusive, int toExclusive, Func<TLocal> localInit, Func<int, ParallelLoopState, TLocal, TLocal> body, Action<TLocal> localFinally);
public static ParallelLoopResult For<TLocal>(long fromInclusive, long toExclusive, Func<TLocal> localInit, Func<long, ParallelLoopState, TLocal, TLocal> body, Action<TLocal> localFinally);
public static ParallelLoopResult For<TLocal>(long fromInclusive, long toExclusive, ParallelOptions parallelOptions, Func<TLocal> localInit, Func<long, ParallelLoopState, TLocal, TLocal> body, Action<TLocal> localFinally);
public static ParallelLoopResult For<TLocal>(int fromInclusive, int toExclusive, ParallelOptions parallelOptions, Func<TLocal> localInit, Func<int, ParallelLoopState, TLocal, TLocal> body, Action<TLocal> localFinally);
最简单的一个是:public static ParallelLoopResult For(int fromInclusive, int toExclusive, Action<int> body);
参数fromInclusive: 是迭代的第一个索引号整数;
参数toExclusive:是迭代的最后一个索引+1的整数;
参数body:是接受单个输入参数的委托,body的代码在每一次迭代中执行一次。
2、实例
using System; using System.Threading.Tasks; // Must use this namespace
//使用Parallel.For语句(并行循环语句)的前提条件:迭代之间彼此独立。 namespace ExampleParallelFor { class Program { static void Main() { //i是索引号,即为For语句中接受的单个参数 Parallel.For( 0, 15, i => Console.WriteLine( “The square of {0} is {1}”, i, i * i ) ); } } }
运行结果:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161806.html原文链接:https://javaforall.cn
相关文章
- JS 中forEach退出循环需要自己制造一个错误
- nodejs中事件循环中的执行顺序
- Js性能优化:循环正序和倒序的性能差异,以及for和foreach的性能比较
- bat批处理 for循环_批处理 for /f
- 两个单向循环链表的合并(带头结点)
- python中使用矢量化替换循环
- Javascript闭包实现Ajax在for循环中正常运行
- WordPress 主题教程 #5:主循环
- Go语言break(跳出循环)
- for循环的特殊写法详解编程语言
- Python for循环语句
- 操作Linux中For In循环的使用(linuxforin)
- 示例利用Oracle For循环实现简单示例(oraclefor循环)
- 进程探索Linux中For循环进程管理(linux中for)
- 表中数据MySQL使用循环遍历输出表中数据(mysql循环输出)
- Linux下如何优雅地使用For循环(linux下for循环)
- MySQL中使用FOR循环快速编写函数(mysql函数for)
- MySQL中loop实现循环语句(mysql中loop)
- 使用循环策略从Redis中取出数据库内容(循环redis取数据库)
- Oracle中使用循环实现运行流程判断(oracle中循环判断)
- JssetInterval与setTimeout(定时执行与循环执行)的代码(可以传入参数)
- c#入门之循环语句使用详解(for循环、do/while)
- Javascript基础教程之for循环