is new.
java.lang.Objectjava.util.concurrent.AbstractExecutorService
public abstract class AbstractExecutorService
Provides default
implementations
implementation
of
ExecutorService
execution methods. This class implements the
submit
,
invokeAny
and
invokeAll
methods using a
RunnableFuture
methods using the default
FutureTask
returned by
class provided in this package. For example, the implementation of
newTaskFor
, which defaults to the
FutureTask
class provided in this package. For example, the implementation of
submit(Runnable)
creates an associated
RunnableFuture
that is executed and returned. Subclasses may override the
newTaskFor
methods to return
RunnableFuture
implementations other than
FutureTask
.
that is executed and returned. Subclasses overriding these methods to use different
Future
implementations should do so consistently for each of these methods.
Extension example
. Here is a sketch of a class that customizes
ThreadPoolExecutor
to use a
CustomTask
class instead of the default
FutureTask
:
public class CustomThreadPoolExecutor extends ThreadPoolExecutor { static class CustomTask<V> implements RunnableFuture<V> {...} protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { return new CustomTask<V>(c); } protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) { return new CustomTask<V>(r, v); } // ... add constructors, etc. }
| Constructor Summary | |
|---|---|
|
AbstractExecutorService
() |
|
| Method Summary | ||
|---|---|---|
|
invokeAll
(
Collection
<? extends
Callable
Executes the given tasks, returning a list of Futures holding their status and results when all complete. |
|
|
invokeAll
(
Collection
<? extends
Callable
Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. |
|
|
invokeAny
(
Collection
<? extends
Callable
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do. |
|
|
invokeAny
(
Collection
<? extends
Callable
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses. |
|
protected
|
newTaskFor
(
Callable
<T> callable)
Returns a
RunnableFuture
for the given callable task.
|
|
protected
|
newTaskFor
(
Runnable
runnable, T value)
Returns a
RunnableFuture
for the given runnable and default value.
|
|
|
submit
(
Callable
<T> task) Submits a value-returning task for execution and returns a Future representing the pending results of the task. |
|
| Future |
submit
(
Runnable
Submits a Runnable task for execution and returns a Future representing that task. |
|
|
submit
(
Runnable
task, T result) Submits a Runnable task for execution and returns a Future representing that
task.
|
|
| Methods inherited from class java.lang. Object |
|---|
| clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait |
| Methods inherited from interface java.util.concurrent. ExecutorService |
|---|
| awaitTermination , isShutdown , isTerminated , shutdown , shutdownNow |
| Methods inherited from interface java.util.concurrent. Executor |
|---|
| execute |
| Constructor Detail |
|---|
public AbstractExecutorService()
| Method Detail |
|---|
newTaskFor
protected <T>
RunnableFuture
<T>
newTaskFor
(
Runnable
runnable, T value)
Returns a
RunnableFuture
for the given runnable and default value.
Parameters:
runnable - the runnable task being wrapped
value - the default value for the returned future
Returns:
a
RunnableFuture
which when run will run the underlying runnable and which, as a
Future
, will yield the given value as its result and provide for cancellation of the underlying task.
Since:
1.6
newTaskFor
protected <T>
RunnableFuture
<T>
newTaskFor
(
Callable
<T> callable)
Returns a
RunnableFuture
for the given callable task.
Parameters:
callable - the callable task being wrapped
Returns:
a
RunnableFuture
which when run will call the underlying callable and which, as a
Future
, will yield the callable's result as its result and provide for cancellation of the underlying task.
Since:
1.6
public Future<?> submit(Runnable task)
The Future's
get
method will return
null
upon successful completion.
a Future representing pending completion of the task
public <T> Future<T> submit(Runnable task,
T result)
task. The Future's
get
method
upon successful completion.
a Future representing pending completion of the task
public <T> Future<T> submit(Callable<T> task)
The Future's
get
method will return the task's result upon successful completion.
If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();
Note: The Executors class includes a set of methods that can convert some other common closure-like objects, for example, PrivilegedAction to Callable form so they can be submitted.
public <T> T invokeAny(Collection<? extends
Callable
<Callable<T>> tasks) throws InterruptedException, ExecutionException
the
tasks
public <T> T invokeAny(Collection<? extends
Callable
<Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
the
public <T> List<Future<T>> invokeAll(Collection<? extends
Callable
<Callable<T>> tasks) throws InterruptedException
public <T> List<Future<T>> invokeAll(Collection<? extends
Callable
<Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
a
cancelled