6.1 Executable objects

So we got to the biggest part of the package. It will describe interfaces for running asynchronous tasks with the ability to obtain results through the Future and Callable interfaces, as well as services and factories for creating thread pools: ThreadPoolExecutor, ScheduledPoolExecutor, ForkJoinPool.

For a better understanding, let's do a little decomposition of interfaces and classes.

6.2 Realizations of executable objects

Future<V>is a wonderful interface for getting the results of an asynchronous operation. The key method here is the get method, which blocks the current thread (with or without a timeout) until an asynchronous operation on another thread has completed. There are also additional methods for canceling the operation and checking the current status. The FutureTask class is often used as an implementation.

RunnableFuture<V>- if Future is an interface for the Client API, then the RunnableFuture interface is already used to start the asynchronous part. Successful completion of the run() method ends the asynchronous operation and allows the results to be retrieved via the get method.

Callable<V>- An extended analogue of the Runnable interface for asynchronous operations. Allows you to return a typed value and throw a checked exception. Even though this interface does not have a run() method, many java.util.concurrent classes support it along with Runnable.

FutureTask<V>— implementation of the Future/RunnableFuture interface. An asynchronous operation is taken as input to one of the constructors in the form of Runnable or Callable objects. The FutureTask class itself is designed to be launched in a worker thread, for example, via new Thread(task).start() or via ThreadPoolExecutor. The results of an asynchronous operation are retrieved via the get(...) method.

Delayed- used for asynchronous tasks that should start in the future, as well as in DelayQueue. Allows you to set the time before the start of an asynchronous operation.

ScheduledFuture<V>- a marker interface that combines the Future and Delayed interfaces.

RunnableScheduledFuture<V>- an interface that combines RunnableFuture and ScheduledFuture. Additionally, you can specify whether the task is a one-time task or should be run at a specified frequency.