我正在开发一个 Java 库,它在 ScheduledExecutorService 中运行计划任务。如果用户指定回调,那么在该任务之后,将调用这些回调。
现在我只有调度程序线程调用回调。这些回调应该在自己的线程池中的自己的 ExecutorService 中执行吗?
在我看来,最好的方法是为客户端提供两种选择:为回调指定执行器和不指定执行器,类似于在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
这样,客户可以选择哪种方法更适合他们的用例。