加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:14:47 所属栏目:百科 来源:网络整理
导读:读了 Parallel.ForEach keeps spawning new threads我仍然怀疑是否是一个正确的方法计数并发线程的数量? 我看到的是,该方法计算Parallel.ForEach中同时输入但不完成的迭代(循环)的数量. 并发线程的数量是否同时传送正确数量的同时运行线程的同义词? 我不是
读了 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实例.也许你需要计算另一种线程,如“管理线程”,所以我不知道我的答案是你寻求的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读