How to interrupt underlying execution of CompletableFuture

前端 未结 4 1614
自闭症患者
自闭症患者 2020-12-29 05:04

I know that CompletableFuture design does not control its execution with interruptions, but I suppose some of you might have this problem. CompletableFutu

4条回答
  •  情歌与酒
    2020-12-29 05:26

    What about this?

    public static  CompletableFuture supplyAsync(final Supplier supplier) {
    
        final ExecutorService executorService = Executors.newFixedThreadPool(1);
    
        final CompletableFuture cf = new CompletableFuture() {
            @Override
            public boolean complete(T value) {
                if (isDone()) {
                    return false;
                }
                executorService.shutdownNow();
                return super.complete(value);
            }
    
            @Override
            public boolean completeExceptionally(Throwable ex) {
                if (isDone()) {
                    return false;
                }
                executorService.shutdownNow();
                return super.completeExceptionally(ex);
            }
        };
    
        // submit task
        executorService.submit(() -> {
            try {
                cf.complete(supplier.get());
            } catch (Throwable ex) {
                cf.completeExceptionally(ex);
            }
        });
    
        return cf;
    }
    

    Simple Test:

        CompletableFuture cf = supplyAsync(() -> {
            try {
                Thread.sleep(1000L);
            } catch (Exception e) {
                System.out.println("got interrupted");
                return "got interrupted";
            }
            System.out.println("normal complete");
            return "normal complete";
        });
    
        cf.complete("manual complete");
        System.out.println(cf.get());
    

    I don't like the idea of having to create an Executor service every time, but maybe you can find a way to reuse the ForkJoinPool.

提交回复
热议问题