我使用 spring boot 版本 2.1.9.RELEASE 和 Java 1.8,并且有兩個 lang 運行進程,我想并行啟動它們。因此我決定使用線程。當我啟動 sumResult 方法時,第二個線程首先啟動,第一個線程等待,直到第二個線程完成。為什么這兩個線程不同時啟動或至少在短時間內(nèi)啟動?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(); }}
1 回答

FFIVE
TA貢獻1797條經(jīng)驗 獲得超6個贊
get()
因為您在提交第二個作業(yè)之前就阻止了第一個作業(yè)的調(diào)用。
submit get submit get
如果你想讓它們并行運行,你需要這樣做
submit submit get get
添加回答
舉報
0/150
提交
取消