Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many Threads in my method?

I'm been searching the web for this answer and cannot find anything that really makes since to me.

I have a program that I am running and I want to count how many threads are in my method at a given time.

I have code in my Main() function:

Parallel.Invoke(MyMethod,MyMethod,MyMethod,MyMethod);


private static void MyMethod()
{
    //how many threads are waiting here???  <--- this is what I am after
    lock (myObj)
    {
        //one thread at a time please
    }
}

Can anyone shed light here??

like image 272
user1158555 Avatar asked Jan 20 '12 15:01

user1158555


People also ask

How do you find the number of threads?

Open Task Manager (press Ctrl+Shift+Esc) Select Performance tab. Look for Cores and Logical Processors (Threads)

How do I find out how many threads I have in Java?

The simplest way to see the number of threads in Java is to use a graphical tool like Java VisualVM. Apart from the application threads, Java VisualVM also lists the GC or any other threads used by the application like JMX threads. Monitoring the number of threads is the most basic feature in Java VisualVM.

How many threads does a process have?

A thread is the unit of execution within a process. A process can have anywhere from just one thread to many threads.

How many threads can my CPU run?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads. And if a CPU is Octal core (i.e., 8 core) it will have 16 threads and vice-versa.


3 Answers

There is no way to directly query how many threads are in a given function. The only way is to do manual tracking

private static int s_threadCount;

private static void MyMethod() {
  Interlocked.Increment(ref s_threadCount);
  try {
    ...
  } finally {
    Interlocked.Decrement(ref s_threadCount);
  }
}

Note: If this method can be recursively entered this won't accurately count the number of threads but instead will count number of threads + number of times they recursively entered the function.

like image 79
JaredPar Avatar answered Sep 21 '22 20:09

JaredPar


The only way to do that would be to add a counter:

static int counter;
... 
static void SomeMethod() {
    int threadsInMethod = Interlocked.Increment(ref counter);
    try {
        code here
    } finally {
        Interlocked.Decrement(ref counter);
    }
}

Caveat: if the method is re-entrant it will overcount itself while nested.

like image 37
Marc Gravell Avatar answered Sep 23 '22 20:09

Marc Gravell


Not expecting many simultaneous enter/leaves and don't care about re-entrancy:

static int _cThreads;
static void SomeMethod()
{
  Interlocked.Increment(ref _cThreads);
  try
  {
    /* blah */
  }
  finally
  {
    Interlocked.Decrement(ref _cThreads);
  }
}

Do care about re-entrancy:

static IDictionary<int, int> _cThreads; // ConcurrentDictionary or alternative thread-safe dictionary
static void SomeMethod()
{
  if(_cThreads.ContainsKey(Thread.CurrentThread.ManagedThreadId))//note that only this thread will hit this key
    _cThreads[Thread.CurrentThread.ManagedThreadId]++
  else
    _cThreads[Thread.CurrentThread.ManagedThreadId] = 1;
  try
  {
    /* blah */
   //When I care about the count then it's _cThreads.Values.Where(v => v != 0).Count()
   //which will mutate while we're trying to count it, but then any
   //answer to this is going to have a degree of staleness
   /*blah*/
  }
  finally
  {
    _cThreads[Thread.CurrentThread.ManagedThreadId]--;
  }
}

If you don't care about re-entrancy, but are expecting lots of simultaneous, but won't want to check the total every time, then use a striped counter. This will be appreciably slower with low contention, but much faster with high contention between cores, and may be applicable to your case.

like image 24
Jon Hanna Avatar answered Sep 23 '22 20:09

Jon Hanna