Controlling a task finishing in an executor

The Java API provides the FutureTask class as a cancelable asynchronous computation. It implements the Runnable and Future interfaces and provides the basic implementation of the Future interface. We can create a FutureTask class using a Callable or Runnable object (Runnable objects doesn't return a result, so we have to pass as parameter too in this case the result that the Future object will return). It provides methods to cancel the execution and obtain the result of the computation. It also provides a method called done() that allows you to execute some code after the finalization of a task executed in an executor. It can be used to make some postprocess operations, such as generating a report, ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.