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 void
Executes the given command at some time in the future.static ScheduledExecutorService
The backingScheduledExecutorService
static boolean
Indicates 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 aScheduledFuture
that 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 afterinitialDelay
theninitialDelay + 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 void
shutdown()
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 aFuture
representing that task, whoseFuture.get()
method will returnnull
upon successful completionstatic <T> Future<T>
Submits a Runnable task for execution and returns aFuture
representing 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 aFuture
representing 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 aScheduledFuture
that 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
ScheduledFuture
that 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
ScheduledFuture
representing pending completion of the task and whoseFuture.get()
method will returnnull
upon 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 afterinitialDelay
theninitialDelay + 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
ScheduledFuture
representing 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 isnull
IllegalArgumentException
- 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
ScheduledFuture
representing 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 isnull
IllegalArgumentException
- 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 theExecutor
implementation- 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 aFuture
representing that task, whoseFuture.get()
method will returnnull
upon successful completion- Parameters:
task
- the task to submit- Returns:
- a
Future
representing 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 aFuture
representing 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
Future
representing 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 aFuture
representing 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
Executors
class includes a set of methods that can convert some other common closure-like objects, for example,PrivilegedAction
toCallable
form so they can be submitted- Type Parameters:
T
- the return type of the callable- Parameters:
task
- the task to submit- Returns:
- a
Future
representing 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'scheckAccess
method denies access.
-
isShutdown
public static boolean isShutdown()Indicates if this ExecutorService is shutdown or in the process of shutting down- Returns:
true
if shutdown
-