如何计算在.NET应用程序的并发线程的数量? [英] How to count the amount of concurrent threads in .NET application?

查看:214
本文介绍了如何计算在.NET应用程序的并发线程的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看了 Parallel.ForEach不断产生新的线程我仍然在怀疑是否是计数并发有线程的数量的正确的方法?



我看到的是什么方法 Parallel.ForEach 结果
是它的并发线程同时输送线程运行的正确数量的数量代名词结果
我不是专家,但我可以想象:




  • 的线程可以被重新使用,而其活性某处交换购买继续。

  • 理论上从事的循环活动线程保持在线程池中的循环完成后但没有再用于其他

  • 或者是什么扭曲实验的纯度(线程计数)的可能性?



反正如何直接计数运行.NET进程的线程,优选在(C#)代码的量?



更新:



所以,如果遵循的Jeppe斯蒂格尼尔森的回答并使用计数

  directThreadsCount = Process.GetCurrentProcess()Threads.Count。 



然后输出,无论是在发行(threadsCount == 7)和调试(threadsCount == 15)模式非常类似:

  [作业0完成。 2个线程,但剩下的== directThreadsCount 7 
[1作业完成。 1线程剩余,但directThreadsCount == 7
[作业2完成。 2个线程,但剩下的== directThreadsCount 7
[工作4完成。 2个线程,但剩下的== directThreadsCount 7
[作业5完成。 2个线程,但剩下的== directThreadsCount 7
[作业3完成。 2个线程,但剩下的== directThreadsCount 7
[作业6完成。 2个线程,但剩下的== directThreadsCount 7
[作业9完成。 2个线程,但剩下的== directThreadsCount 7
[作业7完成。 1线程剩余,但directThreadsCount == 7
[工作8个完整的。 0线程其余但directThreadsCount == 7
FINISHED



也就是说,线程的数量是没有不断下降,告诉上述方法引用不正确,,而 System.Diagnostics.ProcessThread 类的名字是不是在这一点上有效的



是我的结论正确为什么不能使用 ProcessThread



C#控制台应用程序的使用的代码:

 使用系统; 
使用System.Collections.Generic;使用System.Diagnostics程序
;
使用System.Linq的;
使用的System.Threading;使用System.Threading.Tasks
;

命名空间Edit4Posting
{
公共类节点
{

公共节点上{搞定;私人集; }
公共节点(节点以前)
{
上一个=前;
}
}
公共类Edit4Posting
{

公共静态无效的主要(字串[] args)
{
INT concurrentThreads = 0;
INT directThreadsCount = 0;
INT diagThreadCount = 0;

变种工作= Enumerable.Range(0,10);
Parallel.ForEach(工作,委托(INT jobNr)
{
INT threadsRemaining = Interlocked.Increment(REF concurrentThreads);

INT heavyness = jobNr%9;

//给处理器和垃圾收集点事做...
名单<节点个节点=新的List<节点>();
节点电流= NULL;
//为(INT Y = 0; Y< 1024 * 1024 * heavyness; Y ++)
为(INT Y = 0; Y< 1024 * 24 * heavyness; Y ++)
{
电流=​​新节点(电流);
nodes.Add(电流);
}
// *************** ****************
//取消注释下一行给出了:类的名字是不是在这一点上有效的
//diagThreadCount=System.Diagnostics.ProcessThread
directThreadsCount = Process.GetCurrentProcess()Threads.Count;
// ************ ***
threadsRemaining = Interlocked.Decrement(REF concurrentThreads);
Console.WriteLine(
[作业{0}完成{1}线程剩余,但directThreadsCount == {2},
jobNr,threadsRemaining,directThreadsCount);
});
Console.WriteLine(结束);
到Console.ReadLine();
}
}
}


解决方案

有不同的线程,我想。操作系统对你运行的应用程序的线程,可以被计算:

  INT数= Process.GetCurrentProcess()。 Threads.Count; 



这似乎计数 System.Diagnostics.ProcessThread 实例。也许你需要算另一种线程,就像管理线程,所以我不知道我的回答是你寻求的东西。


Having read Parallel.ForEach keeps spawning new threads I am still in doubt whether it is a correct method of counting the number of concurrent there threads?

What I see is that method counts the number of simultaneously entered but not completed iterations (loops) in Parallel.ForEach.
Is it synonym of the number of concurrent threads conveying correct number of simultaneously run threads?
I'm not a specialist but I can imagine that:

  • the threads can be re-used while its activity swapped somewhere for continuing later.
  • theoretically the thread engaged for a loop activity is kept in thread pool after a loop completion but not re-used for another
  • Or what are the possibilities of distorting the purity of experiment (of threads counting)?

Anyway, how to directly count the amount of running threads of .NET process, preferably in (C#) code?

Update:

So, if to follow the Jeppe Stig Nielsen's answer and use for count

directThreadsCount = Process.GetCurrentProcess().Threads.Count;

then output is, both in Release (threadsCount == 7) and Debug (threadsCount == 15) mode is very similar:

[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

That is, the number of threads are not ever decreasing telling that the cited above method is incorrect while System.Diagnostics.ProcessThread gives "Class name is not valid at this point"

Are my conclusions correct and why cannot ProcessThread be used?

The used code of C# console application:

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();
    }
  }
}

解决方案

There are different kinds of threads, I think. The operating system's threads for the application you're running, can be counted with:

int number = Process.GetCurrentProcess().Threads.Count;

It seems to count System.Diagnostics.ProcessThread instances. Maybe you need to count another kind of thread, like "managed threads", so I'm not sure my answer is what you seek.

这篇关于如何计算在.NET应用程序的并发线程的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆