简体   繁体   English

是否可以安排 CompletableFuture?

[英]Is it possible to schedule a CompletableFuture?

Is there any way to schedule CompletableFuture in Java?有没有办法在 Java 中安排 CompletableFuture? What I wanted to do is to schedule a task to be executed with some delay, and chain it with other operations to be performed asynchronously when it completes.我想要做的是安排一个任务以一些延迟执行,并在它完成时将它与其他异步执行的操作链接起来。 So far I didn't find any way to do this.到目前为止,我还没有找到任何方法来做到这一点。

For good ol' Futures we have eg ScheduledExecutorService, where we can schedule a task to be executed with some delay like this:对于好的 ol' Futures,我们有例如 ScheduledExecutorService,我们可以在其中安排一个任务以延迟执行,如下所示:

ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
Future<String> future = scheduledExecutorService.schedule(() -> "someValue", 10, TimeUnit.SECONDS);

Is there any similar way for CompletableFutures? CompletableFutures 有没有类似的方法?

As said , there is support in Java 9.如前所述,Java 9 中有支持。

But it's not hard to create a similar feature under Java 8;但是在 Java 8 下创建类似的特性并不难; you already named the necessary elements:您已经命名了必要的元素:

// 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);
}

which can be used similarly to the Java 9 feature:可以与 Java 9 功能类似地使用:

Executor afterTenSecs = delayedExecutor(10L, TimeUnit.SECONDS);
CompletableFuture<String> future 
  = CompletableFuture.supplyAsync(() -> "someValue", afterTenSecs);

future.thenAccept(System.out::println).join();

Care must be taken to avoid that the shared scheduled executor's threads prevent the JVM from terminating.必须注意避免共享调度执行程序的线程阻止 JVM 终止。 The alternative to a zero core pool size is to use daemon threads:零核心池大小的替代方法是使用守护线程:

static final ScheduledExecutorService SCHEDULER
  = Executors.newSingleThreadScheduledExecutor(r -> {
    Thread t = new Thread(r);
    t.setDaemon(true);
    return t;
  });

If you're using Java 9+ then CompletableFuture#delayedExecutor(long,TimeUnit) may fit your needs:如果您使用的是 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).返回一个新的 Executor,它在给定的延迟后(如果非正则没有延迟)将任务提交给默认的 executor。 Each delay commences upon invocation of the returned executor's execute method.每次延迟都在调用返回的执行程序的execute方法时开始。

Executor delayed = CompletableFuture.delayedExecutor(10L, TimeUnit.SECONDS);
CompletableFuture.supplyAsync(() -> "someValue", delayed)
    .thenAccept(System.out::println)
    .join();

There's also an overload where you can specify the Executor to use in place of the "default executor".还有一个重载,您可以在其中指定要使用的Executor来代替“默认执行器”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 Java 8/11 中组合超过 2 个 CompletableFuture? - Is it possible to combine more than 2 CompletableFuture in Java 8/11? 是否可以通过whenComplete(…)向CompletableFuture添加多个动作? - Is it possible to add multiple actions to a CompletableFuture with whenComplete(…)? Eclipse:是否可以调试CompletableFuture调用的方法? - Eclipse: is it possible to debug a method called by an CompletableFuture? 返回CompletableFuture <Void>或CompletableFuture <?>? - Return CompletableFuture<Void> or CompletableFuture<?>? 是否可以安排Quartz线程每秒运行一次? - Is it possible to schedule Quartz threads to run each second? 可以在运行时为@Schedule注释更改ejb参数吗? - Possible to change ejb parameter at runtime for @Schedule annotation? java CompletableFuture.thenCombine返回CompletableFuture的CompletableFuture - java CompletableFuture.thenCombine returns CompletableFuture of CompletableFuture Java是否可以通过CompletableFuture真正实现“非阻塞”? 是否可以用一个线程异步处理所有内容? - Can Java be truly “non-blocking” with CompletableFuture? Is it possible to handle everything asynchronously with one thread? 使用CompletableFuture进行回调 - Callback with CompletableFuture 带有 CompletableFuture 的 ReentrantLock - ReentrantLock with CompletableFuture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM