线程池执行线程任务花费的时间 Dear 丶 2022-05-23 00:38 179阅读 0赞 public class CounterPoolExecutor extends ThreadPoolExecutor { private AtomicInteger count = new AtomicInteger(0);//统计执行次数 private long startTime = System.currentTimeMillis(); private String funcname = ""; private final static int COUNT = 100; public CounterPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } @Override protected void afterExecute(Runnable r, Throwable t) { //线程执行结束时 int l = count.addAndGet(1); if (l == COUNT) { System.out.println(funcname + "spend time:" + (System.currentTimeMillis() - startTime)); } } public static void main(String[] args) { ExecutorService executorService = new CounterPoolExecutor(50, 100, 60L, TimeUnit.SECONDS, new LinkedBlockingDeque<>()); for (int i = 0; i < 100; i++) { executorService.execute(() -> System.out.print("1")); } } }
还没有评论,来说两句吧...