CompletableFuture(三)

可以对CompletableFuture指定处理完成的时间,如果按时完成则通知,否则抛出超时异常并处理。

    public static void main(String[] args) {
        CompletableFuture<String> responseFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("over");
            return "hello world!";
        });
        failAfter(Duration.ofSeconds(2)).acceptEither(responseFuture, (x) -> {
            System.out.println("responseFuture is over successed! the response is " + x);

        }).exceptionally(throwable -> { //exceptionally必须在最后
            System.out.println("responseFuture is not over on time!");
            return null;
        });
    }

    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    public static <T> CompletableFuture<T> failAfter(Duration duration) {
        final CompletableFuture<T> promise = new CompletableFuture<>();
        scheduler.schedule(() -> {
            final TimeoutException ex = new TimeoutException("Timeout after " + duration);
            return promise.completeExceptionally(ex);
        }, duration.toMillis(), TimeUnit.MILLISECONDS);
        return promise;
    }


你可能感兴趣的:(CompletableFuture(三))