gpt4 book ai didi

java - CompletableFuture.allOf().orTimeout() 的意外行为

转载 作者:行者123 更新时间:2023-12-03 20:47:36 29 4
gpt4 key购买 nike

有两种方法可以强制执行 CompletableFuture 在给定的时间后超时:

  • orTimeout(long timeout, TimeUnit unit)
  • get(long timeout, TimeUnit unit)

  • 我希望他们表现得一样。但是,当应用于 CompletableFuture.allOf(CompletableFuture<?>... cfs) 时,这两种提供超时的方式的行为完全不同!
    基本上,似乎 get()做我所期望的(它阻塞当前线程,直到所有 future 都完成),而 orTimeout()似乎表现得很奇怪(它在第一个 future 完成后尽快解除对当前线程的阻塞)。
    下面是一些代码来演示我观察到的行为:
    import org.junit.Test;

    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.TimeoutException;

    import static java.util.concurrent.TimeUnit.MILLISECONDS;

    public class AllOfWithTimeoutTest {

    public static final int TIMEOUT_IN_MILLIS = 100;

    @Test
    public void allOfOrTimeout1() throws InterruptedException, ExecutionException, TimeoutException {
    getAllOfFuture().get(TIMEOUT_IN_MILLIS, MILLISECONDS);
    }

    @Test
    public void allOfOrTimeout2() throws ExecutionException, InterruptedException {
    getAllOfFuture().orTimeout(TIMEOUT_IN_MILLIS, MILLISECONDS);
    }

    private CompletableFuture<Void> getAllOfFuture() {
    return CompletableFuture.allOf(
    CompletableFuture.runAsync(() -> sleep(1)),
    CompletableFuture.runAsync(() -> sleep(2)),
    CompletableFuture.runAsync(() -> sleep(3)),
    CompletableFuture.runAsync(() -> sleep(4)),
    CompletableFuture.runAsync(() -> sleep(5)),
    CompletableFuture.runAsync(() -> sleep(6)),
    CompletableFuture.runAsync(() -> sleep(7)),
    CompletableFuture.runAsync(() -> sleep(8))
    );
    }

    public static void sleep(int millis) {
    try {
    Thread.sleep(millis);
    System.out.format("Had a nap for %s milliseconds.\r\n", millis);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    };
    }
    allOfOrTimeout1()的打印输出是我所期望的:
    Had a nap for 1 milliseconds.
    Had a nap for 2 milliseconds.
    Had a nap for 3 milliseconds.
    Had a nap for 4 milliseconds.
    Had a nap for 5 milliseconds.
    Had a nap for 6 milliseconds.
    Had a nap for 7 milliseconds.
    Had a nap for 8 milliseconds.
    allOfOrTimeout2()的打印输出不是我所期望的,并且在每次执行时都会略有不同。它通常在前 2 行和 5 行之间打印出来,但从不打印 8 行。这是一个只打印出 2 行的版本:
    Had a nap for 1 milliseconds.
    Had a nap for 2 milliseconds.
    另外,如果我在 IntelliJ 中运行整个测试,最后我会得到一些额外的行:
    Had a nap for 1 milliseconds.
    Had a nap for 2 milliseconds.
    Had a nap for 3 milliseconds.
    Had a nap for 4 milliseconds.
    Had a nap for 5 milliseconds.
    Had a nap for 6 milliseconds.
    Had a nap for 7 milliseconds.
    Had a nap for 8 milliseconds.



    Had a nap for 1 milliseconds.
    Had a nap for 2 milliseconds.

    Had a nap for 3 milliseconds.
    Had a nap for 4 milliseconds.

    Process finished with exit code 0
    Had a nap for
    我的问题是:
  • 这是 orTimeout() 的预期行为吗? ?
  • 如果没有,为什么要这样做?
  • 最佳答案

    Is this the expected behavior for orTimeout() ?


    是的。 OrTimeout是一个非阻塞异步调用,而 get是一个阻塞异步调用。

    关于java - CompletableFuture.allOf().orTimeout() 的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64807597/

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