@Resource
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Test
public void ThreadPoolTest() {
long time1 = System.currentTimeMillis();
CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
test1();
}, threadPoolTaskExecutor);
System.out.println(System.currentTimeMillis()-time1);
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
test2();
}, threadPoolTaskExecutor);
System.out.println(System.currentTimeMillis()-time1);
try {
CompletableFuture.allOf(future1,future2).get();
System.out.println(System.currentTimeMillis()-time1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void test1() {
System.out.println(Thread.currentThread().getName()+"开始执行");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void test2() {
System.out.println(Thread.currentThread().getName()+"开始执行");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
SpringBoot线程池ThreadPoolTaskExecutor的简单使用示例
最新推荐文章于 2024-07-27 22:46:48 发布