Parallel.ForEach 之 MaxDegreeOfParallelism

Reference: Max Degree of Parallelism maximum degree of parallelism configuration

in conclusion:

  1. And the number of threads related
  2. There parallelism settings for

Tests are as follows:

@@@code

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 ( $ "average per second: {( int ) Math.Ceiling (list.Count / stopwatch.Elapsed.TotalSeconds)} " );

Console.Read();

@@#

 

  1. The number of threads does not limit the number of parallel, about 9 sec

  1. 不使用并行,等到花都谢了

  1. 增加线程数,

不限制并行数

并行设为2

并行设为6

 

 

 

 

Guess you like

Origin www.cnblogs.com/QinQouShui/p/12134232.html