您可以使用 ThreadPoolExecutor 的 getPoolSize()、getActiveCount() 和 getQueue().size() 方法来获取当前空闲数量、工作数量和等待的数量。
示例代码如下:
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
// 获取当前线程池中线程数量
int poolSize = executor.getPoolSize();
// 获取正在执行任务的线程数量
int activeCount = executor.getActiveCount();
// 获取等待执行的任务数量
int queueSize = executor.getQueue().size();
System.out.println("当前空闲数量: " + (poolSize - activeCount));
System.out.println("工作数量: " + activeCount);
System.out.println("总数量: " + poolSize);
System.out.println("等待数量: " + queueSize);
注意,这些值的准确性是短暂的,因为线程池的状态在操作过程中可以随时改变。因此这些数据仅供参考,而不是绝对的。