JDK8 CompletableFuture.supplyAsync how to deal with interruptedException

前端 未结 4 560
暖寄归人
暖寄归人 2020-12-17 15:30
CompletableFuture.supplyAsync(
() -> {
    transporter.write(req);
    //here take the value from a blocking queue,will throw a interruptedException
    return re         


        
4条回答
  •  我在风中等你
    2020-12-17 16:07

    I change the code like this.

        CompletableFuture 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;
    

提交回复
热议问题