Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 791
نمايش فراداده

FutureTask<V>java.util.concurrent

Java 5.0runnable

This class is a Runnable wrapper around a Callable object (or around another Runnable). FutureTask is a generic type and the type variable V represents the return type of the wrapped Callable object. AbstractExecutorService uses FutureTask to convert Callable objects passed to the submit( ) method into Runnable objects it can pass to the execute( ) method.

FutureTask also implements the Future interface, which means that the get( ) method waits for the run( ) method to complete and provides access to the result (or exception) of the Callable's execution.

The protected methods set( ) and setException( ) are invoked when the Callable returns a value or throws an exception. done( ) is invoked when the Callable completes or is canceled. Subclasses can override any of these methods to insert hooks for notification, logging, and so on.

Figure 16-85. java.util.concurrent.FutureTask<V>

public class

FutureTask<V> implements Future<V>, Runnable { // Public Constructors public

FutureTask (Callable<V>

callable ); public

FutureTask (Runnable

runnable , V

result ); // Methods Implementing Future public boolean

cancel (boolean

mayInterruptIfRunning ); public V

get ( ) throws InterruptedException, ExecutionException; public V

get (long

timeout , TimeUnit

unit ) throws InterruptedException, ExecutionException, TimeoutException; public boolean

isCancelled ( ); public boolean

isDone ( ); // Methods Implementing Runnable public void

run ( ); // Protected Instance Methods protected void

done ( ); empty protected boolean

runAndReset ( ); protected void

set (V

v ); protected void

setException (Throwable

t ); }