Using MoreExecutors.directExecutor()

In the world of Java async programming, ListenableFuture is a very useful class. One very nice feature about it is you can append callback onto an instance and chain as many callbacks as you want. When adding a callback you need to implicitly or explicitly specify what executor service you want to execute the callback. When you do not want much hassle, MoreExecutors.directExecutor() seems to be a good choice, however by using this service you lose control on what thread will actually run the callback 

Here is the output of running the above snippet of code.

Before submit, main, 1
In run of future1, pool-1-thread-1, 10
In run of future2, pool-1-thread-2, 11
In run of future3, pool-1-thread-3, 12
In callback of future1, main, 1
In callback of future2, main, 1
In callback of future3, pool-1-thread-3, 12

The take away is  – if the future is complete by the time you add the callback, the thread that completes the future will be used to execute the callback; if the future is not yet complete, the caller of addCallback will be the executor. But really you should not have any assumption on which thread will execute the callback, so keep the callback as light-weight as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *