如何计算.NET应用程序中的并发线程数量?

时间:2013-03-13 09:19:18

标签: c# multithreading visual-studio debugging task-parallel-library

阅读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"

时,线程数量不会减少,表明the cited above method不正确

我的结论是否正确以及为什么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();
    }
  }
}

2 个答案:

答案 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

这只是无效的代码。错误消息显示编译器意识到它是类型的名称,但您不能只为变量指定类型名称。