How do I check the number of threads currently running in Java?

I'm looking for a way to see how many threads are currently running

>First > programmatically via windows

Solution

This will give you the total number of threads in the virtual machine:

int nbThreads =  Thread.getAllStackTraces().keySet().size();

Now, if you want all threads to be executing, you can do this:

int nbRunning = 0;
for (Thread t : Thread.getAllStackTraces().keySet()) {
    if (t.getState()==Thread.State.RUNNABLE) nbRunning++;
}

Here are the possible states: thread State javadoc

If you want to see that the running thread is not programming, but using Windows tools, you can use process explorer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>