我使用 spring boot 版本 2.1.9.RELEASE 和 Java 1.8,并且有兩個(gè) lang 運(yùn)行進(jìn)程,我想并行啟動(dòng)它們。因此我決定使用線程。當(dāng)我啟動(dòng) sumResult 方法時(shí),第二個(gè)線程首先啟動(dòng),第一個(gè)線程等待,直到第二個(gè)線程完成。為什么這兩個(gè)線程不同時(shí)啟動(dòng)或至少在短時(shí)間內(nèi)啟動(dòng)?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貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
get()
因?yàn)槟谔峤坏诙€(gè)作業(yè)之前就阻止了第一個(gè)作業(yè)的調(diào)用。
submit get submit get
如果你想讓它們并行運(yùn)行,你需要這樣做
submit submit get get
添加回答
舉報(bào)
0/150
提交
取消