java Callable FutureTask Excecuter: How to listen to finished task

前端 未结 4 1429
我在风中等你
我在风中等你 2020-12-31 16:29

I\'m quite new to executer services. Liked doing everything myself, but I think it\'s time to trust these services.

I want to hand by Executer a R

4条回答
  •  隐瞒了意图╮
    2020-12-31 16:52

    I'd advise taking a look at the com.google.common.util.concurrent package in Guava, specifically the ListenableFuture type and the code related to it.

    Once the next release (r10) is out, it'll be easy to create an ExecutorService that returns ListenableFutures using MoreExecutors.listeningDecorator(ExecutorService). You can also currently wrap your Runnables/Callables in a ListenableFutureTask yourself.

    final ListenableFutureTask task = new ListenableFutureTask(
        runnable, null);
    executor.submit(task);
    task.addListener(new Runnable() {
      public void run() {
        // do whatever
      }
    }, listenerExecutor);
    
        

    提交回复
    热议问题