How to count the amount of concurrent threads in .

2019-01-20 08:18发布

问题:

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

回答1:

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.



回答2:

I'm not a specialist but I can imagine that the threads can be re used while its activity swapped somewhere.

No - apart from some very specific cases (which you almost certainly don't need to worry about) threads aren't use reentrantly. The thread will be reused to run another iteration (or some other task) after the current iteration has completed, but it won't be asked to do something else while your code is running.

So your current approach is reasonable, basically.

Anyway, how to directly count the amount of running threads in .NET?

You can look in perfmon, which will graph it for you. (If the process is under your control, it's simplest to start the process but make it wait for input, then open up perfmon, add a counter from the "Process" options, select "Thread Count" as the counter to add, and restrict it to the process you're interested in from the dropdown. Hit OK, and then make your process start working.)

EDIT: To answer the update:

That is, the number of threads are not ever decreasing telling that the cited above method is incorrect

No, all it shows is that the number of threads actively running your code decreases, but the threads are kept around. That's entirely natural for a thread pool.

why cannot ProcessThread be used?

You're simply using it inappropriately. Jeppe said that the system was counting the instances of ProcessThread whereas your code tries to assign a class to a variable:

diagThreadCount=System.Diagnostics.ProcessThread

That's simply invalid code. The error message shows that the compiler realizes it's the name of a type, but you can't just assign a type name to a variable.