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

标签 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

也就是说,线程数永远不会减少表明 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/

相关文章:

c++ - 为什么在 VCC 2003 中编译需要这么长时间?

c++ - 隐藏 MFC 对话框

c# - 在设计器中移动窗体上的控件时,Visual Studio 2012 突然崩溃

c# - PDFsharp 在图形下绘制文本

c# - await 是否完全阻塞线程?

c - 多线程应用程序中的错误处理

c# - WPF - 无法在自定义用户控件上绑定(bind)工具提示文本

c# - 通过事件参数的异步事件处理程序回调

android - 获取服务中的调用上下文

visual-studio - 编辑 CSS 文件时停止在 Visual Studio 中显示 CSS 大纲窗口