1. 获得堆积任务大小
代码语言:javascript复制public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor)Executors.newFixedThreadPool(2);
for (int i = 0; i < 10; i ) {
threadPoolExecutor.submit(new Thread(() -> {
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}));
}
Thread.sleep(3000);
// out => 等待线程 8
System.out.println(threadPoolExecutor.getQueue().size());
}
获取到堆积大小了,就可以通过打印日志的形式进行输出,也可以通过micrometer prometheus grafana进行完整的监控,可参考 通过micrometer实时监控线程池的各项指标
拓展: ThreadPoolExecutor支持其他数量监控,例如: