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, sending results by e-mail, or releasing some resources. This method is called internally by the FutureTask class when the execution of the task that this FutureTask object is controlling finishes. The method is called after the result of the task is set and its status is changed to isDone, regardless of whether the task has been canceled or finished normally.

By default, this method is empty. You can override the FutureTask class and implement this method to change the behavior. In this recipe, you will learn how to override this method to execute code after the finalization of the tasks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.18.218