gpt4 book ai didi

java - JDK8 CompletableFuture.supplyAsync如何处理interruptedException

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:15 25 4
gpt4 key购买 nike

CompletableFuture.supplyAsync(
() -> {
transporter.write(req);
//here take the value from a blocking queue,will throw a interruptedException
return responseQueue.take();
}, executorService);

常见的处理interruptedException的方法要么是再次中断要么直接抛出interruptedException,但是两者都行不通。有人知道吗?

最佳答案

我这样修改代码。

    CompletableFuture<Rep> result = new CompletableFuture<>();
CompletableFuture.runAsync(() -> {

transporter.write(req);
try {
Rep rep = responseQueue.take();
result.complete(rep);
} catch (InterruptedException e) {
result.completeExceptionally(e);
Thread.currentThread().interrupt();
} catch (Exception e) {
result.completeExceptionally(e);
}

}, executorService);
return result;

关于java - JDK8 CompletableFuture.supplyAsync如何处理interruptedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23184964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com