Name

ExecutorService

Synopsis

This interface extends Executor to add methods to obtain a Future result of the asynchronous execution of a Callable task. It also adds methods for graceful termination or shutdown of an ExecutorService. ThreadPoolExecutor is a useful and highly configurable implementation of this interface. An easy way to obtain instances of this class is through the factory methods of the Executors utility class. Note that ExecutorService is not a generic type; it does not declare any type variables. It does have a number of generic methods, however, that use the type variable T to represent the result type of Callable and Future objects.

The submit( ) method allows you to submit a Callable<T> object to an ExecutorService for execution. Typical ExecutorService implementations invoke the call( ) method of the Callable on another thread, and the return value (of type T) of the method is therefore not available when the call to submit( ) returns. submit( ) therefore returns a Future<T> object: the promise of a return value of type T at some point in the future. See the Future interface for further details.

Two variants on the submit( ) method accept a java.lang.Runnable task instead of a Callable task. The run( ) method of a Runnable has no return value, so the two-argument version of submit( ) accepts a dummy return value of type T and returns a Future<T> that makes this dummy value available when the Runnable has completed running. The other Runnable variant of the submit( ...

Get Java in a Nutshell, 5th 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.