Java 中回调应该发生在哪个线程上?

问题描述 投票:0回答:1

我正在开发一个 Java 库,它在 ScheduledExecutorService 中运行计划任务。如果用户指定回调,那么在该任务之后,将调用这些回调。

现在我只有调度程序线程调用回调。这些回调应该在自己的线程池中的自己的 ExecutorService 中执行吗?

java callback
1个回答
0
投票

在我看来,最好的方法是为客户端提供两种选择:为回调指定执行器和不指定执行器,类似于在CompletableStage中完成的方式:

public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action); // executes action on internal default executor

public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor); // executes action on provided executor

这样,客户可以选择哪种方法更适合他们的用例。

© www.soinside.com 2019 - 2024. All rights reserved.