阅读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
也就是说,当System.Diagnostics.ProcessThread
给出"Class name is not valid at this point"
我的结论是否正确以及为什么?ProcessThread
无法使用
C#console应用程序的使用代码:
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();
}
}
}
答案 0 :(得分:43)
我认为有各种各样的线程。您正在运行的应用程序的操作系统线程可以计算:
int number = Process.GetCurrentProcess().Threads.Count;
似乎计算System.Diagnostics.ProcessThread
个实例。也许你需要计算另一种线程,比如“托管线程”,所以我不确定我的回答是你所寻求的。
答案 1 :(得分:11)
我不是专家,但我可以想象线程可以在其活动交换的地方重新使用。
否 - 除了一些非常特定情况(您几乎肯定不需要担心)线程不会重复使用。在当前迭代完成后,该线程将被重用以运行另一次迭代(或其他任务),但是当代码时,它将不会被要求执行其他操作正在运行。
所以你当前的方法基本上是合理的。
无论如何,如何直接计算.NET中运行线程的数量?
您可以查看perfmon,它会为您绘制图形。 (如果进程在您的控制之下,最简单的方法是启动进程但让它等待输入,然后打开perfmon,从“Process”选项中添加一个计数器,选择“Thread Count”作为要添加的计数器,并限制从下拉列表中您感兴趣的过程。点击OK,然后让您的流程开始工作。)
编辑:回答更新:
也就是说,线程的数量并没有减少,这说明上面引用的方法是不正确的
不,它显示的是,主动运行代码的线程数减少了,但线程保持不变。这对于线程池来说是完全自然的。
为什么不能使用
ProcessThread
?
你只是不恰当地使用它。 Jeppe说系统是计算ProcessThread
的实例,而你的代码试图将一个类分配给一个变量:
diagThreadCount=System.Diagnostics.ProcessThread
这只是无效的代码。错误消息显示编译器意识到它是类型的名称,但您不能只为变量指定类型名称。