Package com.gmt2001.util.concurrent
Class ExecutorService
java.lang.Object
com.gmt2001.util.concurrent.ExecutorService
Provides an interface to a shared
ScheduledExecutorService- Author:
- gmt2001
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidExecutes the given command at some time in the future.static ScheduledExecutorServiceThe backingScheduledExecutorServicestatic booleanIndicates if this ExecutorService is shutdown or in the process of shutting downstatic ScheduledFuture<?>Creates and executes a one-shot action that becomes enabled after the given delaystatic <V> ScheduledFuture<V>Creates and executes aScheduledFuturethat becomes enabled after the given delaystatic ScheduledFuture<?>scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence afterinitialDelaytheninitialDelay + period, theninitialDelay + 2 * period, and so on.static ScheduledFuture<?>scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.static voidshutdown()Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.static Future<?>Submits a Runnable task for execution and returns aFuturerepresenting that task, whoseFuture.get()method will returnnullupon successful completionstatic <T> Future<T>Submits a Runnable task for execution and returns aFuturerepresenting that task, whoseFuture.get()method will return the given result upon successful completionstatic <T> Future<T>Submits a value-returning task for execution and returns aFuturerepresenting the pending results of the task, whoseFuture.get()method will return the task's result upon successful completion
-
Method Details
-
executorService
The backingScheduledExecutorService- Returns:
- the backing
ScheduledExecutorService
-
schedule
Creates and executes aScheduledFuturethat becomes enabled after the given delay- Type Parameters:
V- the return type of the callable- Parameters:
callable- the function to executedelay- the time from now to delay executionunit- the time unit of the delay parameter- Returns:
- a
ScheduledFuturethat can be used to extract result or cancel - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if callable isnull
-
schedule
Creates and executes a one-shot action that becomes enabled after the given delay- Parameters:
command- the task to executedelay- the time from now to delay executionunit- the time unit of the delay parameter- Returns:
- a
ScheduledFuturerepresenting pending completion of the task and whoseFuture.get()method will returnnullupon completion - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if command isnull
-
scheduleAtFixedRate
public static ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence afterinitialDelaytheninitialDelay + period, theninitialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute- Parameters:
command- the task to executeinitialDelay- the time to delay first executionperiod- the period between successive executionsunit- the time unit of the initialDelay and period parameters- Returns:
- a
ScheduledFuturerepresenting pending completion of the task, and whoseFuture.get()method will throw an exception upon cancellation - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if command isnullIllegalArgumentException- if period less than or equal to zero
-
scheduleWithFixedDelay
public static ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor- Parameters:
command- the task to executeinitialDelay- the time to delay first executiondelay- the delay between the termination of one execution and the commencement of the nextunit- the time unit of the initialDelay and delay parameters- Returns:
- a
ScheduledFuturerepresenting pending completion of the task, and whoseFuture.get()method will throw an exception upon cancellation - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if command isnullIllegalArgumentException- if delay less than or equal to zero
-
execute
Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of theExecutorimplementation- Parameters:
command- the runnable task- Throws:
RejectedExecutionException- if this task cannot be accepted for executionNullPointerException- if command isnull
-
submit
Submits a Runnable task for execution and returns aFuturerepresenting that task, whoseFuture.get()method will returnnullupon successful completion- Parameters:
task- the task to submit- Returns:
- a
Futurerepresenting pending completion of the task - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if the task isnull
-
submit
Submits a Runnable task for execution and returns aFuturerepresenting that task, whoseFuture.get()method will return the given result upon successful completion- Type Parameters:
T- the return type of result- Parameters:
task- the task to submitresult- the result to return- Returns:
- a
Futurerepresenting pending completion of the task - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if the task isnull
-
submit
Submits a value-returning task for execution and returns aFuturerepresenting the pending results of the task, whoseFuture.get()method will return the task's result upon successful completionIf you would like to immediately block waiting for a task, you can use constructions of the form
result = exec.submit(aCallable).get();Note: The
Executorsclass includes a set of methods that can convert some other common closure-like objects, for example,PrivilegedActiontoCallableform so they can be submitted- Type Parameters:
T- the return type of the callable- Parameters:
task- the task to submit- Returns:
- a
Futurerepresenting pending completion of the task - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if the task isnull
-
shutdown
public static void shutdown()Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down- Throws:
SecurityException- if a security manager exists and shutting down this ExecutorService may manipulate threads that the caller is not permitted to modify because it does not holdRuntimePermission("modifyThread"), or the security manager'scheckAccessmethod denies access.
-
isShutdown
public static boolean isShutdown()Indicates if this ExecutorService is shutdown or in the process of shutting down- Returns:
trueif shutdown
-