Parallel.ForEach使用示例
发布日期:2025-05-01 22:05:38 浏览次数:2 分类:技术文章

本文共 811 字,大约阅读时间需要 2 分钟。

新建一个.NET Core控制台程序,代码如下:

using System;using System.Collections.Generic;using System.Threading;using System.Threading.Tasks;namespace NetCoreParallel{    class Program    {        static void Main(string[] args)        {            var numbersToShow = new List
() { 1, 2, 3, 4, 5, 6 }; Parallel.ForEach(numbersToShow, number => { Thread.Sleep(3000); Console.WriteLine($"Parallel ForEach is now displaying number: {number.ToString()}"); }); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Parallel ForEach finished."); Console.WriteLine("Press key contiune..."); Console.ReadKey(); } }}

执行后结果如下:

因为Parallel.ForEach为并行执行,所以再次执行时6个数字出现的顺序可能会不一样。

 

上一篇:Parallel.ForEach的基础使用
下一篇:Papyrus项目常见问题解决方案

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月26日 14时41分30秒