在springboot项目中使用线程池
//spring boot会自动注入
@Autowired
private ThreadPoolTaskExecutor executor;
@GetMapping("duoxiancheng")
public String duoxiancheng() throws ExecutionException, InterruptedException {
//异步执行
CompletableFuture<Void> renwu1 = CompletableFuture.runAsync(() -> {
msg += "任务一,";
}, executor);
//异步执行
CompletableFuture<Void> renwu2 = CompletableFuture.runAsync(() -> {
msg += "任务二,";
}, executor);
msg += "主任务,";
//会等待任务执行完毕才会往下执行
CompletableFuture.allOf(renwu1,renwu2).get();
System.out.println(msg);
return msg;
}