CountDownLatch countDownLatch = new CountDownLatch(5);
ExecutorService threadPool = new ThreadPoolExecutor(
5,
5,
1,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
for (int i = 0; i < 5; i++) {
int finalI = i;
int finalI1 = i;
threadPool.submit(()->{
try {
System.out.println("----线程"+ finalI+"启动时间为"+System.currentTimeMillis());
//在此处理业务
System.out.println("线程"+ finalI1 +"执行");
System.out.println("线程"+ finalI1 +"结束时间:"+System.currentTimeMillis());
}finally {
countDownLatch.countDown();
}
});
}
//关闭线程处理
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
//关闭线程池
threadPool.shutdown();
多线程操作
于 2022-01-17 16:28:27 首次发布