Parallel.ForEach 之 MaxDegreeOfParallelism
Posted qinqoushui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Parallel.ForEach 之 MaxDegreeOfParallelism相关的知识,希望对你有一定的参考价值。
参考:Max Degree of Parallelism最大并行度配置
结论:
- 与设置的线程数有关
- 有设置的并行度有关
测试如下:
System.Threading.ThreadPool.SetMinThreads(20, 20);
System.Threading.ThreadPool.SetMinThreads(50, 50);
var list = GetIPByMask(IPAddress.Parse("192.168.10.1"), IPAddress.Parse("255.255.255.0"));
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism=6}, a =>
{
var status = Ping(a.ToString(), (ip, ex) => Console.WriteLine($"ping {ip},{ex.Message}"), 1000);
Console.WriteLine($"at {DateTime.Now.Second} ping {a.ToString()} {status.ToString()}");
}
);
//foreach (var a in list)
//{
// var status = Ping(a.ToString(), (ip, ex) => Console.WriteLine($"ping {ip},{ex.Message}"), 1000);
// Console.WriteLine($"at {DateTime.Now.Second} ping {a.ToString()} {status.ToString()}");
//}
Console.WriteLine($"平均每秒处理:{(int)Math.Ceiling( list.Count/stopwatch.Elapsed.TotalSeconds)}" );
Console.Read();
@@#
?
- 不设置线程数,不限制并行数,每秒约9个
- 不使用并行,等到花都谢了
- 增加线程数,
不限制并行数
并行设为2
并行设为6
?
?
?
?
以上是关于Parallel.ForEach 之 MaxDegreeOfParallelism的主要内容,如果未能解决你的问题,请参考以下文章