简体   繁体   中英

How to get the number of current worker threads in a JVM threadpool?

I am using the threadpool classes in java Base class library. Specifically Executors.newFixedThreadPool() etc.

In my application i want to log metrics on how many threads are running (actually doing user supplied work) vs waiting for work.

I looked at the hooks provided by the classes, but could not find any way to do it.

I can of course implement something like that by putting my work unit in the worker thread around a try/finally and incrementing/decrementing a global counter. But was wondering if there was something out of the box that i am missing.

To get total number of threads:

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM