Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the active thread count?

I have a program which calls a C++ library. The program processes has a large number of threads (50 - 60). Most of them seem to be created in C++ and I suspect most are suspended/waiting.

How do I find how many of these threads are active at a given point in time?

like image 403
DayOne Avatar asked Apr 30 '10 11:04

DayOne


People also ask

What is active thread count?

The activeCount() method of thread class is used to return the number of active threads in the current thread's thread group. The value returned is only an estimate because the number of threads may change dynamically while this method traverses internal data structures.

How do I check my thread status?

Use Thread. currentThread(). isAlive() to see if the thread is alive[output should be true] which means thread is still running the code inside the run() method or use Thread.

How do I get current thread?

A thread can be created by implementing the Runnable interface and overriding the run() method. The current thread is the currently executing thread object in Java. The method currentThread() of the Thread class can be used to obtain the current thread. This method requires no parameters.


2 Answers

To actually determine the count of active threads, it's necessary to check the ThreadState property of each thread.

((IEnumerable)System.Diagnostics.Process.GetCurrentProcess().Threads)
    .OfType<System.Diagnostics.ProcessThread>()
    .Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running)
    .Count();
like image 132
Nathan Avatar answered Oct 13 '22 21:10

Nathan


You could use Process Explorer to inspect threads. It will tell you in realtime how much CPU each is consuming, and can give you individual stack traces, which will indicate what they are blocked on.

like image 28
Marcelo Cantos Avatar answered Oct 13 '22 21:10

Marcelo Cantos