我使用 spring boot 版本 2.1.9.RELEASE 和 Java 1.8,并且有两个 lang 运行进程,我想并行启动它们。因此我决定使用线程。当我启动 sumResult 方法时,第二个线程首先启动,第一个线程等待,直到第二个线程完成。
为什么这两个线程不同时启动或至少在短时间内启动?
private void sumResult(String year, String month, String day) throws
ExecutionException, InterruptedException {
ExecutorCompletionService<Boolean> completionService = new
ExecutorCompletionService<>(Executors.newCachedThreadPool());
// First thread
mut.initialise(year, month, day);
boolean mutCompleted = completionService.submit(
()-> mut.sum(),true).get();
// Second thread
apt.initialise(year, month, day);
boolean aptCompleted = completionService.submit(
()-> apt.sum(), true).get();
// On completion of both thread
if(mutCompleted && aptCompleted ){
mixAndPrint();
}
}
FFIVE
相关分类