阅读Parallel.ForEach 不断产生新线程后,我仍然怀疑它是否是计算并发线程数的正确方法?
我看到的是该方法计算同时输入但未完成的迭代(循环)的数量Parallel.ForEach
。
它是传达正确数量的同时运行线程的并发线程数量的同义词吗?
我不是专家,但我可以想象:
- 线程可以在其活动在某处交换以供以后继续时重复使用。
- 理论上,用于循环活动的线程在循环完成后保留在线程池中,但不会重新用于另一个
- 或者扭曲实验(线程计数)纯度的可能性是什么?
无论如何,如何直接计算.NET进程的运行线程数,最好是(C#)代码?
更新:
因此,如果遵循Jeppe Stig Nielsen 的回答并使用计数
directThreadsCount = Process.GetCurrentProcess().Threads.Count;
那么输出就是,在 Release (threadsCount == 7) 和 Debug (threadsCount == 15) 模式下都非常相似:
[Job 0 complete. 2 threads remaining but directThreadsCount == 7
[Job 1 complete. 1 threads remaining but directThreadsCount == 7
[Job 2 complete. 2 threads remaining but directThreadsCount == 7
[Job 4 complete. 2 threads remaining but directThreadsCount == 7
[Job 5 complete. 2 threads remaining but directThreadsCount == 7
[Job 3 complete. 2 threads remaining but directThreadsCount == 7
[Job 6 complete. 2 threads remaining but directThreadsCount == 7
[Job 9 complete. 2 threads remaining but directThreadsCount == 7
[Job 7 complete. 1 threads remaining but directThreadsCount == 7
[Job 8 complete. 0 threads remaining but directThreadsCount == 7
FINISHED
也就是说,线程的数量并没有减少,说明上面引用的方法是不正确的,而System.Diagnostics.ProcessThread
给出"Class name is not valid at this point"
我的结论是否正确,为什么不能?ProcessThread
使用
C#控制台应用程序的使用代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Edit4Posting
{
public class Node
{
public Node Previous { get; private set; }
public Node(Node previous)
{
Previous = previous;
}
}
public class Edit4Posting
{
public static void Main(string[] args)
{
int concurrentThreads = 0;
int directThreadsCount = 0;
int diagThreadCount = 0;
var jobs = Enumerable.Range(0, 10);
Parallel.ForEach(jobs, delegate(int jobNr)
{
int threadsRemaining = Interlocked.Increment(ref concurrentThreads);
int heavyness = jobNr % 9;
//Give the processor and the garbage collector something to do...
List<Node> nodes = new List<Node>();
Node current = null;
//for (int y = 0; y < 1024 * 1024 * heavyness; y++)
for (int y = 0; y < 1024 * 24 * heavyness; y++)
{
current = new Node(current);
nodes.Add(current);
}
//*******************************
//uncommenting next line gives: "Class name is not valid at this point"
//diagThreadCount=System.Diagnostics.ProcessThread
directThreadsCount = Process.GetCurrentProcess().Threads.Count;
//*******************************
threadsRemaining = Interlocked.Decrement(ref concurrentThreads);
Console.WriteLine(
"[Job {0} complete. {1} threads remaining but directThreadsCount == {2}",
jobNr, threadsRemaining, directThreadsCount);
});
Console.WriteLine("FINISHED");
Console.ReadLine();
}
}
}