亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 Spring Boot 中使用 ExecutorCompletionService

為什么 Spring Boot 中使用 ExecutorCompletionService

守著一只汪 2023-12-30 16:13:35
我使用 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();         }}
查看完整描述

1 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

get()因為您在提交第二個作業之前就阻止了第一個作業的調用。

submit
get
submit
get

如果你想讓它們并行運行,你需要這樣做

submit
submit
get
get


查看完整回答
反對 回復 2023-12-30
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號