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?
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.
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.
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With