是否可以安排 CompletableFuture?
Is it possible to schedule a CompletableFuture?
有什么方法可以在 Java 中安排 CompletableFuture 吗?
我想做的是安排一个任务以一定的延迟执行,并在它完成时将其与其他异步执行的操作链接起来。到目前为止我还没有找到任何方法来做到这一点。
为了更好的期货,我们有例如ScheduledExecutorService,我们可以在其中安排一个任务以像这样的延迟执行:
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
Future<String> future = scheduledExecutorService.schedule(() -> "someValue", 10, TimeUnit.SECONDS);
CompletableFutures有没有类似的方法?
如果您使用的是 Java 9+,那么 CompletableFuture#delayedExecutor(long,TimeUnit)
可能满足您的需求:
Returns a new Executor that submits a task to the default executor after the given delay (or no delay if non-positive). Each delay commences upon invocation of the returned executor's execute
method.
Executor delayed = CompletableFuture.delayedExecutor(10L, TimeUnit.SECONDS);
CompletableFuture.supplyAsync(() -> "someValue", delayed)
.thenAccept(System.out::println)
.join();
还有 an overload,您可以在其中指定 Executor
来代替 "default executor"。
,在 Java 9.
中有支持
但在 Java8 下创建类似的功能并不难;您已经命名了必要的元素:
// prefer this constructor with zero core threads for a shared pool,
// to avoid blocking JVM exit
static final ScheduledExecutorService SCHEDULER = new ScheduledThreadPoolExecutor(0);
static Executor delayedExecutor(long delay, TimeUnit unit)
{
return delayedExecutor(delay, unit, ForkJoinPool.commonPool());
}
static Executor delayedExecutor(long delay, TimeUnit unit, Executor executor)
{
return r -> SCHEDULER.schedule(() -> executor.execute(r), delay, unit);
}
可以类似于 Java 9 功能使用:
Executor afterTenSecs = delayedExecutor(10L, TimeUnit.SECONDS);
CompletableFuture<String> future
= CompletableFuture.supplyAsync(() -> "someValue", afterTenSecs);
future.thenAccept(System.out::println).join();
必须注意避免共享调度执行程序的线程阻止 JVM 终止。零核心池大小的替代方法是使用守护线程:
static final ScheduledExecutorService SCHEDULER
= Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
});
有什么方法可以在 Java 中安排 CompletableFuture 吗? 我想做的是安排一个任务以一定的延迟执行,并在它完成时将其与其他异步执行的操作链接起来。到目前为止我还没有找到任何方法来做到这一点。
为了更好的期货,我们有例如ScheduledExecutorService,我们可以在其中安排一个任务以像这样的延迟执行:
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
Future<String> future = scheduledExecutorService.schedule(() -> "someValue", 10, TimeUnit.SECONDS);
CompletableFutures有没有类似的方法?
如果您使用的是 Java 9+,那么 CompletableFuture#delayedExecutor(long,TimeUnit)
可能满足您的需求:
Returns a new Executor that submits a task to the default executor after the given delay (or no delay if non-positive). Each delay commences upon invocation of the returned executor's
execute
method.
Executor delayed = CompletableFuture.delayedExecutor(10L, TimeUnit.SECONDS);
CompletableFuture.supplyAsync(() -> "someValue", delayed)
.thenAccept(System.out::println)
.join();
还有 an overload,您可以在其中指定 Executor
来代替 "default executor"。
但在 Java8 下创建类似的功能并不难;您已经命名了必要的元素:
// prefer this constructor with zero core threads for a shared pool,
// to avoid blocking JVM exit
static final ScheduledExecutorService SCHEDULER = new ScheduledThreadPoolExecutor(0);
static Executor delayedExecutor(long delay, TimeUnit unit)
{
return delayedExecutor(delay, unit, ForkJoinPool.commonPool());
}
static Executor delayedExecutor(long delay, TimeUnit unit, Executor executor)
{
return r -> SCHEDULER.schedule(() -> executor.execute(r), delay, unit);
}
可以类似于 Java 9 功能使用:
Executor afterTenSecs = delayedExecutor(10L, TimeUnit.SECONDS);
CompletableFuture<String> future
= CompletableFuture.supplyAsync(() -> "someValue", afterTenSecs);
future.thenAccept(System.out::println).join();
必须注意避免共享调度执行程序的线程阻止 JVM 终止。零核心池大小的替代方法是使用守护线程:
static final ScheduledExecutorService SCHEDULER
= Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
});