Futures.addCallback 如何保证在执行未来任务的线程中执行回调?

how Futures.addCallback guarantee the callback to be executed at the thread executing the future task?

代码如下:

final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(4));

final Callable<String> asyncTask = new Callable<String>() {
    @Override
    public String call() throws Exception {
    return TestFuture.computeResult();
    }
};

final int listSize = 10;

final List<ListenableFuture<String>> listenableFutures = Lists.newArrayListWithExpectedSize(listSize);
for (int i = 0; i < listSize; i++) {
    listenableFutures.add(executor.submit(asyncTask));
}

for (final ListenableFuture<String> listenableFuture2 : listenableFutures) {
    Futures.addCallback(listenableFuture2, new FutureCallback<String>() {
    @Override
    public void onSuccess(final String result) {
        System.out.println("callback success with " + result + " at " + Thread.currentThread().getName());
    }

    @Override
    public void onFailure(final Throwable thrown) {
        System.out.println("callback failed with " + thrown.getMessage());
    }
    });
}

我不知道回调是如何安排在将执行 asyncTask 的线程上执行的?

我可以看到 Futures.addCallback 调用 addCallback(future, callback, MoreExecutors.sameThreadExecutor());,但我不知道 MoreExecutors.sameThreadExecutor 如何保证这一点?

sameThreadExecutor() 只是执行传递给它的内联 execute(Runnable) 方法的任何 Runnable:也就是说,它只是简单地调用 run()。它根本不做任何与线程相关的事情。并且回调 Runnable 被传递到执行 asyncTask 的同一线程上的关联 Executor,因为 ListeningExecutorService 有效地将您的 asyncTask 包装在 FutureTask 在调用其 done() 方法时调用所有回调。

注意:不是实际上保证你所有的回调都将在执行asyncTask的线程上执行。例如,如果您在 asyncTask 完成后添加回调 ,它将在您添加回调的线程上执行(假设您没有提供Executor 除了要使用的回调 sameThreadExecutor()