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( ) method takes no return value and returns a Future<?> value. The get( ) method of this Future object returns null when the Runnable is done. Other ExecutorService methods execute Callable objects synchronously. invokeAll( ) is passed a java.util.Collection of Callable<T> tasks . It executes them and blocks until all have completed, or until an optionally specified timeout has elapsed. invokeAll( ) returns the results of the tasks as a List of Future<T> objects. Note that a Callable<T> task can complete either by returning a result of type T or by throwing an exception. invokeAny( ) is also passed a Collection of Callable<T> objects. It blocks until any one of these Callable tasks has returned a value of type T and returns that value. Tasks that terminate by throwing an exception are ignored. If all tasks throw an exception, invokeAny( ) throws an ExecutionException . Before invokeAny( ) returns, it cancels the execution of any still-running Callable tasks. Like invokeAll( ) , invokeAny( ) has a variant with a timeout value. ExecutorService defines several methods for gracefully shutting down the service. shutdown( ) puts the ExecutorService into a special state in which no new tasks may be submitted for execution, but all currently running tasks continue running. isShutdown( ) returns true if the ExecutorService has entered this state. awaitTermination( ) blocks until all executing tasks in an ExecutorService that was shut down are completed (or until a specified timeout elapses). Once this has occurred, the isTerminated( ) method returns true . The shutdownNow( ) method shuts down an ExecutorService more abruptly: it attempts to abort all currently executing tasks (typically via Thread.interrupt( ) ) and returns a List of the tasks that have not yet started executing. Figure 16-84. java.util.concurrent.ExecutorServicepublic interface ExecutorService extends Executor { // Public Instance Methods boolean awaitTermination (long timeout , TimeUnit unit ) throws InterruptedException; <T> java.util.List<Future<T>> invokeAll (java.util.Collection<Callable<T>> tasks ) throws InterruptedException; <T> java.util.List<Future<T>> invokeAll (java.util.Collection<Callable<T>> tasks , long timeout , TimeUnit unit ) throws InterruptedException; <T> T invokeAny (java.util.Collection<Callable<T>> tasks ) throws InterruptedException, ExecutionException; <T> T invokeAny (java.util.Collection<Callable<T>> tasks , long timeout , TimeUnit unit ) throws InterruptedException, ExecutionException, TimeoutException; boolean isShutdown ( ); boolean isTerminated ( ); void shutdown ( ); java.util.List<Runnable> shutdownNow ( ); <T> Future<T> submit (Callable<T> task ); Future<?> submit (Runnable task ); <T> Future<T> submit (Runnable task , T result ); } ImplementationsAbstractExecutorService , ScheduledExecutorService Passed ToExecutors.unconfigurableExecutorService( ) Returned ByExecutors.{newCachedThreadPool( ) , newFixedThreadPool( ) , newSingleThreadExecutor( ) , unconfigurableExecutorService( )} |