Future

Future

Future interface

  • ThreadPoolExecutor interfaces

// submit a Runnable task
Future<?> submit(Runnable task);

// submit a Callable task
<T> Future<T> submit(Callable<T> task);

// submit a Runnable task and its result 
<T> Future<T> submit(Runnable task, T result);
  • Future interface methods

FutureTask

  • FutureTask implements Runnable and Future interface.

CompletableFuture

  • Benefits:

    • No need to manually create / maintain threads

    • More clear semantics on thread relationships with completionStage such as f3 = f1.thenCombine(f2, ()->{})

  • Constructor methods

  • CompletionStage: Could be used to specify the relationship between fn, consumer or action

    • fn means Function, support both parameters T and return value R.

    • consumer means Consumer supports only parameters T.

    • action means Runnable, does not support T or R.

CompletionService

  • Def: Implements a blockingQueue inside. When the future result is available, it will be put future task inside the blocking queue.

  • It has the following two constructors:

    • ExecutorCompletionService(Executor executor)

    • ExecutorCompletionService(Executor executor, BlockingQueue> completionQueue

  • It provides the following interface methods

Last updated

Was this helpful?