public class MdcScheduledExecutorService extends Object implements ScheduledExecutorService
Constructor and Description |
---|
MdcScheduledExecutorService(ScheduledExecutorService scheduledExecutor,
Map<String,String> fixedContext)
Initializes a new instance of the
MdcScheduledExecutorService class |
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.
|
<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()
Returns true if this executor has been shut down.
|
boolean |
isTerminated()
Returns true if all tasks have completed following shut down.
|
<V> ScheduledFuture<V> |
schedule(Callable<V> callable,
long delay,
TimeUnit unit)
Creates and executes a ScheduledFuture that becomes enabled after the
given delay.
|
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit)
Creates and executes a one-shot action that becomes enabled
after the given delay.
|
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 after
initialDelay then initialDelay+period, then
initialDelay + 2 * period, and so on.
|
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.
|
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.
|
<T> Future<T> |
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 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.
|
public MdcScheduledExecutorService(ScheduledExecutorService scheduledExecutor, Map<String,String> fixedContext)
MdcScheduledExecutorService
classscheduledExecutor
- A ScheduledExecutorService
wrapped by the current MdcScheduledExecutorService
instancefixedContext
- The MDC fixed context used by current executorpublic ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
command
- the task to executedelay
- the time from now to delay executionunit
- the time unit of the delay parameterRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if command is nullpublic <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
callable
- the function to executedelay
- the time from now to delay executionunit
- the time unit of the delay parameterRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if callable is nullpublic ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduleAtFixedRate
in interface ScheduledExecutorService
command
- the task to executeinitialDelay
- the time to delay first executionperiod
- the period between successive executionsunit
- the time unit of the initialDelay and period parametersRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if command is nullIllegalArgumentException
- if period less than or equal to zeropublic ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
scheduleWithFixedDelay
in interface ScheduledExecutorService
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 parametersRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if command is nullIllegalArgumentException
- if delay less than or equal to zeropublic void shutdown()
This method does not wait for previously submitted tasks to
complete execution. Use awaitTermination
to do that.
shutdown
in interface ExecutorService
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 hold RuntimePermission
("modifyThread"),
or the security manager's checkAccess method
denies access.public List<Runnable> shutdownNow()
This method does not wait for actively executing tasks to
terminate. Use awaitTermination
to
do that.
There are no guarantees beyond best-effort attempts to stop
processing actively executing tasks. For example, typical
implementations will cancel via Thread.interrupt()
, so any
task that fails to respond to interrupts may never terminate.
shutdownNow
in interface ExecutorService
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 hold RuntimePermission
("modifyThread"),
or the security manager's checkAccess method
denies access.public boolean isShutdown()
isShutdown
in interface ExecutorService
public boolean isTerminated()
isTerminated
in interface ExecutorService
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
awaitTermination
in interface ExecutorService
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if interrupted while waitingpublic <T> Future<T> submit(Callable<T> task)
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.
submit
in interface ExecutorService
task
- the task to submitRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if the task is nullpublic <T> Future<T> submit(Runnable task, T result)
submit
in interface ExecutorService
task
- the task to submitresult
- the result to returnRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if the task is nullpublic Future<?> submit(Runnable task)
submit
in interface ExecutorService
task
- the task to submitRejectedExecutionException
- if the task cannot be
scheduled for executionNullPointerException
- if the task is nullpublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
Future.isDone()
is true for each
element of the returned list.
Note that a completed task could have
terminated either normally or by throwing an exception.
The results of this method are undefined if the given
collection is modified while this operation is in progress.invokeAll
in interface ExecutorService
tasks
- the collection of tasksInterruptedException
- if interrupted while waiting, in
which case unfinished tasks are cancelled.NullPointerException
- if tasks or any of its elements are nullRejectedExecutionException
- if any task cannot be
scheduled for executionpublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
Future.isDone()
is true for each
element of the returned list.
Upon return, tasks that have not completed are cancelled.
Note that a completed task could have
terminated either normally or by throwing an exception.
The results of this method are undefined if the given
collection is modified while this operation is in progress.invokeAll
in interface ExecutorService
tasks
- the collection of taskstimeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if interrupted while waiting, in
which case unfinished tasks are cancelledNullPointerException
- if tasks, any of its elements, or
unit are nullRejectedExecutionException
- if any task cannot be scheduled
for executionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
invokeAny
in interface ExecutorService
tasks
- the collection of tasksInterruptedException
- if interrupted while waitingNullPointerException
- if tasks or any element task
subject to execution is nullIllegalArgumentException
- if tasks is emptyExecutionException
- if no task successfully completesRejectedExecutionException
- if tasks cannot be scheduled
for executionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
invokeAny
in interface ExecutorService
tasks
- the collection of taskstimeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if interrupted while waitingNullPointerException
- if tasks, or unit, or any element
task subject to execution is nullTimeoutException
- if the given timeout elapses before
any task successfully completesExecutionException
- if no task successfully completesRejectedExecutionException
- if tasks cannot be scheduled
for executionpublic void execute(Runnable command)
execute
in interface Executor
command
- the runnable taskRejectedExecutionException
- if this task cannot be
accepted for execution.NullPointerException
- if command is nullCopyright © 2016–2025. All rights reserved.