一、解决 CompletableFuture.runAsync 异步线程不执行的问题
方式一:指定线程池
private static final ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
public static void main(String[] args) throws InterruptedException, ExecutionException {
CompletableFuture.runAsync(() ->{
try {
method();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
},cachedThreadPool);
System.out.println("main +++++++++++++++++++++++");
}
private static void method() throws InterruptedException {
System.out.println("start==================");
Thread.sleep(10000);
System.out.println("end====================");
}
参考:https://blog.csdn.net/weixin_40796429/article/details/130631883?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-130631883-blog-127760017.235%5Ev38%5Epc_relevant_sort_base2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-130631883-blog-127760017.235%5Ev38%5Epc_relevant_sort_base2&utm_relevant_index=1