網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
簡(jiǎn)介:任務(wù)并行庫(kù)(Task Parellel Library)是BCL的一個(gè)類庫(kù),極大的簡(jiǎn)化了并行編程。
使用任務(wù)并行庫(kù)執(zhí)行循環(huán)
C#當(dāng)中我們一般使用for和foreach執(zhí)行循環(huán),有時(shí)候我們呢的循環(huán)結(jié)構(gòu)每一次的迭代需要依賴以前一次的計(jì)算或者行為。但是有時(shí)候則不需要。如果迭代之間彼此獨(dú)立,并且程序運(yùn)行在多核處理器上,如果能將不同的迭代放到不同的處理器上并行處理,則會(huì)獲益匪淺。Parallel.For和Parallel.ForEach就是為此而生的。
①使用Parallel.For 聲明如下:
這里可以看到 toExclusive這個(gè)參數(shù),它是不含的, 在使用的時(shí)候傳入?yún)?shù)要注意下。
舉個(gè)例子:
static void Main(string[] args)
{
Parallel.For(0, 5, i =>
{
//打印平方
Console.WriteLine("The Square of {0} is {1}", i, i * i);
}
);
Console.ReadKey();
}
執(zhí)行結(jié)果:
The Square of 0 is 0
The Square of 2 is 4
The Square of 1 is 1
The Square of 4 is 16
The Square of 3 is 9
從執(zhí)行結(jié)果上我們可以看到,它不是按順序執(zhí)行的。那么問(wèn)題來(lái)了,怎么讓結(jié)果保持有序?
我們可以通過(guò)一個(gè)數(shù)組來(lái)存儲(chǔ)執(zhí)行的結(jié)果,例如下面的例子:
static void Main(string[] args)
{
const int maxValues = 5;
int[] Squares = new int[maxValues];
Parallel.For(0, maxValues , i =>Squares[i] = i*i );
for (int i = 0; i < maxValues; i++) Console.WriteLine("Square of {0} is {1}", i, Squares[i]);
Console.ReadKey();
}
我們首先定義了一個(gè)數(shù)組,然后由于數(shù)組的下標(biāo)已經(jīng)定下來(lái)了,所以每次執(zhí)行都會(huì)存入具體的位置,然后遍歷結(jié)果的數(shù)組,就得到了有順序的結(jié)果。
②使用Parallel.ForEach
最簡(jiǎn)單的實(shí)現(xiàn),聲明如下:
舉例:
static void Main(string[] args)
{
string[] squares = new string[]
{"We", "hold", "these", "truths", "to", "be", "self-evident", "that", "all", "men", "are", "created", "equal"};
Parallel.ForEach(squares,
i => Console.WriteLine(string.Format("'{0}' has {1} letters", i, i.Length)));
Console.ReadKey();
}
結(jié)果:
'We' has 2 letters
'hold' has 4 letters
'these' has 5 letters
'to' has 2 letters
'truths' has 6 letters
'self-evident' has 12 letters
'that' has 4 letters
'be' has 2 letters
'men' has 3 letters
'are' has 3 letters
'created' has 7 letters
'equal' has 5 letters
'all' has 3 letters
這里同樣可以看到,不是按順序遍歷的。
原文鏈接:https://www.cnblogs.com/dcz2015/p/11015163.html
相關(guān)推薦
- 2023-05-07 Pygame顯示文字的實(shí)現(xiàn)示例_python
- 2022-09-29 Python組合數(shù)據(jù)類型詳解_python
- 2022-05-26 ASP.NET?Core依賴注入詳解_實(shí)用技巧
- 2023-04-01 react組件實(shí)例屬性state詳解_React
- 2022-06-04 Python?os和os.path模塊詳情_(kāi)python
- 2022-04-06 如何將Python編譯成C語(yǔ)言_python
- 2022-05-19 C++inline函數(shù)的特性你了解嗎_C 語(yǔ)言
- 2022-07-28 Python常用Web框架Django、Flask與Tornado介紹_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支