CompletableFuture with ForkJoinPool and customized ExecutorService - shinevien/misc GitHub Wiki
#1. FYI from answer: https://stackoverflow.com/questions/54035090/behaviour-of-forkjoinpool-in-completablefuture-supplyasync
ForkJoinPool uses daemon threads that does not prevent JVM from exiting. On the other hand the threads in the ExecutorService created by Executors are non-daemon threads, hence it keeps JVM from exiting until you explicitly shutdown the thread pool.
Also notice that in your example you need to shutdown the pool at the end in order to terminate the JVM.
executorService.shutdown();
So, one solution would be to keep the main thread waiting for few seconds until your computation is completed like so,
Thread.sleep(4000);
#2. commonPool API FYI: https://blog.csdn.net/coffeelifelau/article/details/53908072
public static ForkJoinPool commonPool()
Returns the common pool instance. This pool is statically constructed; its run state is unaffected by attempts toshutdown() orshutdownNow(). However this pool and anyongoing processing are automatically terminated upon programSystem.exit(int). Any program that relies on asynchronous task processing to complete before program termination should invokecommonPool().awaitQuiescence, before exit.
返回common pool实例。此线程池是静态构造的,其运行不受shutdown() 或shutdownNow()影响,但此线程池和任何不间断运行的任务都将在调用System.exit(int)后自动终止。任何依赖异步任务的程序在未完成任务时应在系统退出前调用commonPool().awaitQuiescence。