Java set a callback from ExecutorService

前端 未结 4 876
一整个雨季
一整个雨季 2020-12-31 09:29

I have a fixedThreadPool that I am using to run a bunch of worker threads to achieve parallel execution of a task with many components.

When all threads have finishe

4条回答
  •  长发绾君心
    2020-12-31 09:54

    If using Google Guava is an option, you could utilize the ListenableFuture interface in the following manner:

    1. Convert an ExecutorService to a ListeningExecutorService via MoreExecutors.listeningDecorator(existingExecutorService)
    2. The submit(Callable) method of ListeningExecutorService has been narrowed to return a ListenableFuture, which is a subinterface of Future.
    3. ListenableFuture has an addListener() method so you can register a callback to be run when the future is completed.

提交回复
热议问题