Active threads in ExecutorService

后端 未结 6 1545
盖世英雄少女心
盖世英雄少女心 2020-12-23 19:16

Any ideas how to determine the number of active threads currently running in an ExecutorService?

6条回答
  •  难免孤独
    2020-12-23 19:20

    Check the sourcecode for Executors.newFixedThreadPool():

    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue());
    

    ThreadPoolExecutor has a getActiveCount() method. So you might either cast the ExecutorService to ThreadPoolExecutor, or use the above code directly to obtain one. You can then invoke getActiveCount().

提交回复
热议问题