已阅读Parallel.ForEach keeps spawning new threads我仍然怀疑这是否是计算并发线程数的正确方法?
我看到的是该方法计算了 Parallel.ForEach
中同时输入但未完成的迭代(循环)的次数。
它是并发线程数的同义词,表示同时运行线程的正确数量吗?
我不是专家,但我可以想象:
- 可以重新使用线程,同时将其事件交换到某个地方以便稍后继续。
- 理论上,参与循环事件的线程在循环完成后保留在线程池中,但不会重新用于另一个
- 或者扭曲实验(线程计数)纯度的可能性有多大?
无论如何,如何直接统计.NET进程的运行线程数,最好是在(C#)代码中?
更新:
所以,如果按照Jeppe Stig Nielsen's answer并用于计数
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
也就是说,线程数永远不会减少表明 the cited above method是不正确的 而 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();
}
}
}
最佳答案
我认为有不同种类的线程。您正在运行的应用程序的操作系统线程可以计算为:
int number = Process.GetCurrentProcess().Threads.Count;
它似乎计算 System.Diagnostics.ProcessThread
实例。也许您需要计算另一种线程,例如“托管线程”,所以我不确定我的答案是否符合您的要求。
关于c# - 如何计算.NET 应用程序中的并发线程数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15381174/