第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從 CompletableFuture 調(diào)用 ExecutorService.shutdownNow

從 CompletableFuture 調(diào)用 ExecutorService.shutdownNow

狐的傳說 2021-08-04 17:20:41
當(dāng)已經(jīng)運(yùn)行的任務(wù)之一引發(fā)異常時(shí),我需要取消所有計(jì)劃但尚未運(yùn)行的 CompletableFuture 任務(wù)。嘗試了以下示例,但大多數(shù)情況下 main 方法不會(huì)退出(可能是由于某種類型的死鎖)。public static void main(String[] args) {    ExecutorService executionService = Executors.newFixedThreadPool(5);    Set< CompletableFuture<?> > tasks = new HashSet<>();    for (int i = 0; i < 1000; i++) {        final int id = i;        CompletableFuture<?> c = CompletableFuture        .runAsync( () -> {            System.out.println("Running: " + id);             if ( id == 400 ) throw new RuntimeException("Exception from: " + id);        }, executionService )        .whenComplete( (v, ex) -> {             if ( ex != null ) {                System.out.println("Shutting down.");                executionService.shutdownNow();                System.out.println("shutdown.");            }        } );        tasks.add(c);    }    try{         CompletableFuture.allOf( tasks.stream().toArray(CompletableFuture[]::new) ).join();     }catch(Exception e) {         System.out.println("Got async exception: " + e);     }finally {         System.out.println("DONE");     }        }最后的打印輸出是這樣的:Running: 402Running: 400Running: 408Running: 407Running: 406Running: 405Running: 411Shutting down.Running: 410Running: 409Running: 413Running: 412shutdown.嘗試shutdownNow在單獨(dú)的線程上運(yùn)行方法,但在大多數(shù)情況下,它仍然會(huì)出現(xiàn)相同的死鎖。知道什么可能導(dǎo)致這種僵局嗎?您認(rèn)為在CompletableFuture拋出異常時(shí)取消所有已安排但尚未運(yùn)行的s的最佳方法是什么?正在考慮迭代tasks并調(diào)用cancel每個(gè)CompletableFuture. 但我不喜歡這個(gè)的是CancellationException從join.
查看完整描述

2 回答

?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

另一個(gè)僅依賴的解決方案CompletableFuture是使用“取消者”未來,這將導(dǎo)致所有未完成的任務(wù)在完成時(shí)被取消:


Set<CompletableFuture<?>> tasks = ConcurrentHashMap.newKeySet();

CompletableFuture<Void> canceller = new CompletableFuture<>();


for(int i = 0; i < 1000; i++) {

    if (canceller.isDone()) {

        System.out.println("Canceller invoked, not creating other futures.");

        break;

    }

    //LockSupport.parkNanos(10);

    final int id = i;

    CompletableFuture<?> c = CompletableFuture

            .runAsync(() -> {

                //LockSupport.parkNanos(1000);

                System.out.println("Running: " + id);

                if(id == 400) throw new RuntimeException("Exception from: " + id);

            }, executionService);

    c.whenComplete((v, ex) -> {

        if(ex != null) {

            canceller.complete(null);

        }

    });

    tasks.add(c);

}

canceller.thenRun(() -> {

    System.out.println("Cancelling all tasks.");

    tasks.forEach(t -> t.cancel(false));

    System.out.println("Finished cancelling tasks.");

});


查看完整回答
反對(duì) 回復(fù) 2021-08-04
  • 2 回答
  • 0 關(guān)注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)