0

I'm developing a Java library that runs a scheduled task in a ScheduledExecutorService. If the user specifies callback(s), then after that task, those callback(s) will be invoked.

Right now I just have the scheduler thread invoking the callbacks. Should those callbacks execute in their own ExecutorService in their own thread pool?

2
  • 5
    It does't matter, so long as it's documented - the recipient then becomes responsible for managing it based on their needs and context Commented Sep 25, 2023 at 21:41
  • That makes sense. I've decided for now to have a separate ExcutorService for handling the callbacks.
    – leros
    Commented Sep 26, 2023 at 20:05

1 Answer 1

1

In my opinion, the best approach would be to provide the client with both options: with an Executor specified for the callback and without it, similar to how it's done in 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

This way, the client can choose which approach is more convenient for their use case.

1
  • That makes sense for complete flexibility. For now, I'm going to have a separate ExecutorService for callbacks to execute on.
    – leros
    Commented Sep 26, 2023 at 22:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.