網站首頁 編程語言 正文
一、簡介:
Parallel類提供了數據和任務的并行性;
Paraller.For()方法類似于C#的for循環語句,也是多次執行一個任務。使用Paraller.For()方法,可以并行運行迭代,迭代的順序沒有定義。在For()方法中,前兩個參數是固定的,這兩個參數定義了循環的開頭和結束。首先描述它的第一個方法For(int,int,Action<int>),前面兩個參數代表循環的開頭和介紹,第三個參數是個委托,整數參數是循環的迭代次數,該參數被傳遞給委托引用的方法。Paraller.For()方法的返回類型是ParallelLoopResult結構,它提供了循環是否結束的信息和最低迭代的索引(返回一個表示從中調用 Break 語句的最低迭代的整數)。
二、Paraller.For(int,int,Action<int>):
代碼演示:
public static void Test()
{
ParallelLoopResult result = Parallel.For(0, 10, i =>
{
Console.WriteLine("迭代次數:{0},任務ID:{1},線程ID:{2}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10);
});
Console.WriteLine("是否完成:{0}", result.IsCompleted);
Console.WriteLine("最低迭代:{0}", result.LowestBreakIteration);
}
運行結果:
三、Paraller.For(int,int,Action<int,ParallelLoopState>):
可以看到,上面委托方法運行了10次,順序也是不能被保證的。但是最低迭代并沒有數據出來,這是因為他是返回調用 Break 語句的最低迭代的整數,在這我們并沒有break。如果需要才執行過程中提前中斷For()方法,就可以使用ParallelLoopState來實現,For(int,int,Action<int,ParallelLoopState>)。
代碼演示:
public static void Test()
{
ParallelLoopResult result = Parallel.For(0, 10, (i, state) =>
{
Console.WriteLine("迭代次數:{0},任務ID:{1},線程ID:{2}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10);
if (i > 5)
state.Break();
});
Console.WriteLine("是否完成:{0}", result.IsCompleted);
Console.WriteLine("最低迭代:{0}", result.LowestBreakIteration);
}
運行結果:
四、Parallel.ForEach()
Paraller.ForEach()方法遍歷實現了IEnumerable的集合,其方法類似于 foreach的語句,但以異步方式遍歷,這里也沒有確定遍歷順序。首先描述它的第一個方法,Paraller.ForEach<TSource>(IEnumerable<TSource>,Action<TSource>),先看下例子:
代碼演示:
public static void Test()
{
string[] data = { "str1", "str2", "str3" };
ParallelLoopResult result = Parallel.ForEach<string>(data, str =>
{
Console.WriteLine(str);
});
Console.WriteLine("是否完成:{0}", result.IsCompleted);
Console.WriteLine("最低迭代:{0}", result.LowestBreakIteration);
}
運行結果:
五、Parallel.ForEach<TSource>
它也可以像For一樣傳入迭代次數和ParallelLoopState的,方法是ForEach<TSource>(IEnumerable<TSource> source, Action<TSource, ParallelLoopState, long> body)
代碼演示:
public static void Test()
{
string[] data = { "str1", "str2", "str3", "str4", "str5" };
ParallelLoopResult result = Parallel.ForEach<string>(data, (str, state, i) =>
{
Console.WriteLine("迭代次數:{0},{1}", i, str);
if (i > 3)
state.Break();
});
Console.WriteLine("是否完成:{0}", result.IsCompleted);
Console.WriteLine("最低迭代:{0}", result.LowestBreakIteration);
}
運行結果:
六、Parallel.Invoke()
Parallel.Invoke()方法,它提供了任務并行性模式。Paraller.Invoke()方法允許傳遞一個Action委托數組,在其中可以指定應運行的方法,看下面的例子:
代碼演示:
public static void Test()
{
Parallel.Invoke(() =>
{
Thread.Sleep(100);
Console.WriteLine("method1");
}, () =>
{
Thread.Sleep(10);
Console.WriteLine("method2");
});
}
運行結果:
七、總結
Parallel.For()和Paraller.ForEach()方法在每次迭代中調用相同的代碼,而Parallel.Invoke()方法允許同時調用不同的方法。Parallel.ForEach()用于數據并行性,Parallel.Invoke()用于任務并行性。
原文鏈接:https://www.cnblogs.com/wml-it/p/14794736.html
相關推薦
- 2023-01-21 C#實現Word轉換RTF的示例代碼_C#教程
- 2022-05-22 查看Docker容器的信息的方法實現_docker
- 2024-07-13 SpringBoot入門(解決JDK8不存在問題)
- 2023-01-29 Python安裝Talib庫的詳細圖文教程_python
- 2022-03-20 C++中vector容器的注意事項總結_C 語言
- 2023-04-02 深入了解Go語言中web框架的中間件運行機制_Golang
- 2022-01-16 DOM簡介及獲取元素方法屬性總結
- 2022-07-26 react如何添加less環境配置_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支