public class JPPFExecutorService extends JobListenerAdapter implements ExecutorService
ExecutorService wrapper around a JPPFClient.
This executor has two modes in which it functions:
1) Standard mode: in this mode each task or set of tasks submitted via one of the
invokeXXX() or submit() methods is sent immediately to the server in its own JPPF job.
2) Batch mode: the JPPFExecutorService can be configured to only send tasks to the server
when a number of tasks, submitted via one of the invokeXXX() or submit() methods,
has been reached, or when a timeout specified in milliseconds has expired, or a combination of both.
This facility is designed to optimize the task execution throughput, especially when many individual tasks are submitted
using one of the submit() methods. This way, the tasks are sent to the server as a single job,
instead of one job per task, and the execution will fully benefit from the parallel features of the JPPF server, including
scheduling, load-balancing and parallel I/O.
In batch mode, the following behavior is to be noted:
invokeXXX() methods, they are guaranteed
to be all sent together in the same JPPF job. This is the one exception to the batch size threshold.| Constructor and Description |
|---|
JPPFExecutorService(JPPFClient client)
Initialize this executor service with the specified JPPF client.
|
JPPFExecutorService(JPPFClient client,
int batchSize,
long batchTimeout)
Initialize this executor service with the specified JPPF client, batch size and batch tiemout.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs,
or the current thread is interrupted, whichever happens first.
|
void |
execute(Runnable command)
Executes the given command at some time in the future.
|
int |
getBatchSize()
Get the minimum number of tasks that must be submitted before they are sent to the server.
|
long |
getBatchTimeout()
Get the maximum time to wait before the next batch of tasks is to be sent for execution.
|
ExecutorServiceConfiguration |
getConfiguration()
Get the configuration for this executor service.
|
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks)
Executes the given tasks, returning a list of Futures holding their status and results when all complete.
|
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
Executes the given tasks, returning a list of Futures holding their status and results
when all complete or the timeout expires, whichever happens first.
|
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks)
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do.
|
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
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.
|
boolean |
isShutdown()
Determine whether this executor has been shut down.
|
boolean |
isTerminated()
Determine whether all tasks have completed following shut down.
|
ExecutorServiceConfiguration |
resetConfiguration()
Reset the configuration for this executor service to a blank state.
|
JPPFExecutorService |
setBatchSize(int batchSize)
Set the minimum number of tasks that must be submitted before they are sent to the server.
|
JPPFExecutorService |
setBatchTimeout(long batchTimeout)
Set the maximum time to wait before the next batch of tasks is to be sent for execution.
|
void |
shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
|
List<Runnable> |
shutdownNow()
Attempts to stop all actively executing tasks, halts the processing of waiting tasks,
and returns a list of the tasks that were awaiting execution.
This implementation simply waits for all submitted tasks to terminate, due to the complexity of stopping remote tasks. |
<T> Future<T> |
submit(Callable<T> task)
Submit a value-returning task for execution and returns a Future representing the pending results of the task.
|
Future<?> |
submit(Runnable task)
Submits a Runnable task for execution and returns a Future representing that task.
|
<T> Future<T> |
submit(Runnable task,
T result)
Submits a Runnable task for execution and returns a Future representing that task that will upon completion return the given result.
|
jobDispatched, jobEnded, jobReturned, jobStartedpublic JPPFExecutorService(JPPFClient client)
client - the JPPFClient to use for job submission.public JPPFExecutorService(JPPFClient client, int batchSize, long batchTimeout)
client - the JPPFClient to use for job submission.batchSize - the minimum number of tasks that must be submitted before they are sent to the server.batchTimeout - the maximum time to wait before the next batch of tasks is to be sent for execution.public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
invokeAll in interface ExecutorServiceT - the type of results returned by the tasks.tasks - the tasks to execute.InterruptedException - if interrupted while waiting, in which case unfinished tasks are cancelled.NullPointerException - if tasks or any of its elements are null.RejectedExecutionException - if any task cannot be scheduled for execution.public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
invokeAll in interface ExecutorServiceT - the type of results returned by the tasks.tasks - the tasks to execute.timeout - the maximum time to wait.unit - the time unit of the timeout argument.InterruptedException - if interrupted while waiting, in which case unfinished tasks are cancelled.NullPointerException - if tasks or any of its elements are null.RejectedExecutionException - if any task cannot be scheduled for execution.public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
invokeAny in interface ExecutorServiceT - the type of results returned by the tasks.tasks - the tasks to execute.InterruptedException - if interrupted while waiting.NullPointerException - if tasks or any of its elements are null.IllegalArgumentException - if tasks empty.ExecutionException - if no task successfully completes.RejectedExecutionException - if tasks cannot be scheduled for execution.public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
invokeAny in interface ExecutorServiceT - the type of results returned by the tasks.tasks - the tasks to execute.timeout - the maximum time to wait.unit - the time unit of the timeout argument.InterruptedException - if interrupted while waiting.NullPointerException - if tasks or any of its elements are null.IllegalArgumentException - if tasks empty.ExecutionException - if no task successfully completes.RejectedExecutionException - if tasks cannot be scheduled for execution.TimeoutException - if the given timeout elapses before any task successfully completes.public <T> Future<T> submit(Callable<T> task)
submit in interface ExecutorServiceT - the type of result returned by the task.task - the task to execute.public Future<?> submit(Runnable task)
submit in interface ExecutorServicetask - the task to execute.ExecutorService.submit(java.lang.Runnable)public <T> Future<T> submit(Runnable task, T result)
submit in interface ExecutorServiceT - the type of result returned by the task.task - the task to execute.result - the result to return .public void execute(Runnable command)
execute in interface Executorcommand - the command to execute.Executor.execute(java.lang.Runnable)public boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface ExecutorServicetimeout - the maximum time to wait.unit - the time unit of the timeout argument.InterruptedException - if interrupted while waiting.public boolean isShutdown()
isShutdown in interface ExecutorServicepublic boolean isTerminated()
isTerminated in interface ExecutorServicepublic void shutdown()
shutdown in interface ExecutorServicepublic List<Runnable> shutdownNow()
shutdownNow in interface ExecutorServicepublic int getBatchSize()
public JPPFExecutorService setBatchSize(int batchSize)
batchSize - the batch size as an int.public long getBatchTimeout()
public JPPFExecutorService setBatchTimeout(long batchTimeout)
batchTimeout - the timeout as a long.public ExecutorServiceConfiguration getConfiguration()
ExecutorServiceConfiguration instance.public ExecutorServiceConfiguration resetConfiguration()
ExecutorServiceConfiguration instance.Copyright © 2005-2019 JPPF Team.