Java 8 CompletableFuture,Stream和Timeouts

时间:2016-12-30 07:18:39

标签: java concurrency java-8 java-stream completable-future

我正在尝试使用CompletableFutureStream同时处理一些数据量 到目前为止我有:

public static void main(String[] args) throws InterruptedException, ExecutionException {
    System.out.println("start");

    List<String> collect = Stream.of("1", "2", "3", "4", "5",
            "6", "7")
            .map(x -> CompletableFuture.supplyAsync(getStringSupplier(x)))
            .collect(Collectors.toList())
            .stream()
            .map(CompletableFuture::join)
            .collect(Collectors.toList());
    System.out.println("stop out!");
}


public static Supplier<String> getStringSupplier(String text) {
    return () -> {

        System.out.println("start " + text);
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("stop " + text);
        return "asd" + text;
    };
}

输出很好:

start start 1 start 4 start 3 start 2 start 5 start 6 start 7 stop 4 stop 1 stop 5 stop 2 stop 6 stop 3 stop 7 stop out!

但是现在我想为该工作添加超时。让我们说它应该在1 SECOND之后取消。并将null或其他值返回collect列表。 (我希望有一些值表明原因)。

我怎样才能实现这一目标?

提前感谢您的帮助。

4 个答案:

答案 0 :(得分:3)

我找到了这样做的方法:

 private static final ScheduledExecutorService scheduler =
        Executors.newScheduledThreadPool(
                1,
                new ThreadFactoryBuilder()
                        .setDaemon(true)
                        .setNameFormat("failAfter-%d")
                        .build());

public static void main(String[] args) throws InterruptedException, ExecutionException {
    System.out.println("start");
    final CompletableFuture<Object> oneSecondTimeout = failAfter(Duration.ofSeconds(1))
            .exceptionally(xxx -> "timeout exception");
    List<Object> collect = Stream.of("1", "2", "3", "4", "5", "6", "7")
            .map(x -> CompletableFuture.anyOf(createTaskSupplier(x)
                    , oneSecondTimeout))
            .collect(Collectors.toList())
            .stream()
            .map(CompletableFuture::join)
            .collect(Collectors.toList());
    System.out.println("stop out!");
    System.out.println(collect);
}

public static CompletableFuture<String> createTaskSupplier(String x) {
    return CompletableFuture.supplyAsync(getStringSupplier(x))
            .exceptionally(xx -> "PROCESSING ERROR : " + xx.getMessage());
}


public static Supplier<String> getStringSupplier(String text) {
    return () -> {

        System.out.println("start " + text);
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (text.equals("1")) {
            throw new RuntimeException("LOGIC ERROR");
        }
        try {
            if (text.equals("7"))
                TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("stop " + text);
        return "result " + text;
    };
}

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(), MILLISECONDS);
    return promise;
}

它返回:

 start
 start 1
 start 3
 start 4
 start 2
 start 5
 start 6
 start 7
 stop 6
 stop 4
 stop 3
 stop 5
 stop 2
 stop out!
 [PROCESSING ERROR : java.lang.RuntimeException: LOGIC ERROR, result 2, result 3, result 4, result 5, result 6, timeout exception]`

您如何看待这个问题,您能发现该解决方案的任何缺陷吗?

答案 1 :(得分:2)

对于不受Java 8限制的其他人,您可以使用Java 9中引入的completeOnTimeout方法。

List<String> collect = Stream.of("1", "2", "3", "4", "5", "6", "7")
        .map(x -> CompletableFuture.supplyAsync(getStringSupplier(x))
                .completeOnTimeout(null , 1, SECONDS))
        .filter(Objects::nonNull)
        .collect(toList())
        .stream()
        .map(CompletableFuture::join)
        .collect(toList());

答案 2 :(得分:0)

您可以将作业包装在另一个CompletableFuture中,如果超过给定时间,它将发出TimeoutException。如果要特别处理它,可以将TimeoutException catch块分开。

    List<String> collect = null;
    try {
        collect = CompletableFuture.supplyAsync(() ->
                Stream.of("1", "2", "3", "4", "5",
                        "6", "7")
                        .map(x -> CompletableFuture.supplyAsync(getStringSupplier(x)))
                        .collect(Collectors.toList())
                        .stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList())
        ).get(5, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        e.printStackTrace();
        //separate out the TimeoutException if you want to handle it differently 

    }

    System.out.println(collect); //would be null in case of any exception

答案 3 :(得分:0)

你可以尝试CompletableFuture重载的supplyAsync方法和执行器参数 (CompletableFuture.supplyAsync(getStringSupplier(x),timeoutExecutorService)) ,可以引用link来获取timeoutExecutorService