T
- the types of results that the submitted Callable
s are to have.public final class AsynchronousCallableCompletionService<T>
extends java.lang.Object
CompletionService
and provides an abstraction for
submitting a set of Callable
s, along with a single callback class, CallableResultHandler
,
for processing the result/exception of every Callable
.
class MyAsyncProcessor
{
final static int NUMBER_OF_THREADS = 1000;
CallableResultHandler<Boolean> callableHandlerImpl = new CallableResultHandler<Boolean>()
{
public void onCompletion(Boolean resultFromASingleCallable) {
// process result
}
public void onException(ExecutionException executionException) {
// process exception
}
};
AsynchronousCallableCompletionService<Boolean> theDecider =
new AsynchronousCallableCompletionService<Boolean>(
Executors.newFixedThreadPool(NUMBER_OF_THREADS), callableHandlerImpl);
MyAsyncProcessor(List<Callable<Boolean>> districts) {
for (Callable<Boolean> district: districts) {
theDecider.submit(district);
}
}
}
Constructor and Description |
---|
AsynchronousCallableCompletionService(java.util.concurrent.ExecutorService executor,
CallableResultHandler<T> asyncContentLevelCallback)
Instantiates an
AsynchronousCallableCompletionService that will use the
provided executor to process Callable s. |
Modifier and Type | Method and Description |
---|---|
boolean |
isShutdown()
Whether the service is in a shutdown state.
|
boolean |
isTerminated()
Whether the service is in a terminated state.
|
void |
shutdownNow(boolean mayInterruptIfRunning)
Cancels all scheduled
Callable s including the ones
currently in progress and shuts down the completion service. |
void |
submit(java.util.concurrent.Callable<T> task)
Submits a callable that is to be scheduled with the completion service.
|
public AsynchronousCallableCompletionService(java.util.concurrent.ExecutorService executor, CallableResultHandler<T> asyncContentLevelCallback)
AsynchronousCallableCompletionService
that will use the
provided executor
to process Callable
s.executor
- the executor to be used with the completion serviceasyncContentLevelCallback
- the callback object for processing individual resultspublic void submit(java.util.concurrent.Callable<T> task)
Callable
terminate without exceptions,
CallableResultHandler#onCompletion(T)
will be called.
Otherwise, CallableResultHandler.onException(ExecutionException)
will be called.task
- the callable task to be submitted for execution.RejectedExecutionException
- when service is shut down.public void shutdownNow(boolean mayInterruptIfRunning)
Callable
s including the ones
currently in progress and shuts down the completion service.
The semantics of the cancellation policy for every Future
is the same as the semantics of mayInterruptIfRunning
in Future.cancel(boolean)
. The underlying ExecutorService
is
shut down. A best-effort-only guarantee is made for currently executing tasks.mayInterruptIfRunning
- Future.cancel(boolean)
,
ExecutorService.shutdownNow()
public boolean isShutdown()
true
, if the service is shut down.ExecutorService.isShutdown()
public boolean isTerminated()
shutdownNow(boolean)
true
if the service is terminated.ExecutorService.isShutdown()