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

前端之家收集整理的这篇文章主要介绍了c# – 如何计算.NET应用程序中的并发线程数量?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
读了 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在这一点上无效”时,线程数不会减少,告知the cited above method不正确

我的结论是否正确,为什么不能使用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#相关文章