public class ManuallyTriggeredScheduledExecutorService extends Object implements ScheduledExecutorService
ScheduledExecutorService
implementation for testing purposes. It spawns no
threads, but lets you trigger the execution of tasks manually.
This class is helpful when implementing tests tasks synchronous and control when they run, which would otherwise asynchronous and require complex triggers and latches to test.
Constructor and Description |
---|
ManuallyTriggeredScheduledExecutorService() |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit) |
void |
execute(Runnable command) |
Collection<ScheduledFuture<?>> |
getNonPeriodicScheduledTask() |
Collection<ScheduledFuture<?>> |
getPeriodicScheduledTask() |
Collection<ScheduledFuture<?>> |
getScheduledTasks() |
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks) |
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit) |
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks) |
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit) |
boolean |
isShutdown() |
boolean |
isTerminated() |
int |
numQueuedRunnables()
Gets the number of Runnables currently queued.
|
<V> ScheduledFuture<V> |
schedule(Callable<V> callable,
long delay,
TimeUnit unit) |
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit) |
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit) |
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit) |
void |
shutdown() |
List<Runnable> |
shutdownNow() |
<T> Future<T> |
submit(Callable<T> task) |
Future<?> |
submit(Runnable task) |
<T> Future<T> |
submit(Runnable task,
T result) |
void |
trigger()
Triggers the next queued runnable and executes it synchronously.
|
void |
triggerAll()
Triggers all
queuedRunnables . |
void |
triggerNonPeriodicScheduledTask()
Triggers a single non-periodically scheduled task.
|
void |
triggerNonPeriodicScheduledTasks() |
void |
triggerNonPeriodicScheduledTasksWithRecursion()
Triggers all non-periodically scheduled tasks.
|
void |
triggerPeriodicScheduledTasks() |
void |
triggerScheduledTasks()
Triggers all registered tasks.
|
public ManuallyTriggeredScheduledExecutorService()
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduleAtFixedRate
in interface ScheduledExecutorService
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
scheduleWithFixedDelay
in interface ScheduledExecutorService
public void shutdown()
shutdown
in interface ExecutorService
public List<Runnable> shutdownNow()
shutdownNow
in interface ExecutorService
public boolean isShutdown()
isShutdown
in interface ExecutorService
public boolean isTerminated()
isTerminated
in interface ExecutorService
public boolean awaitTermination(long timeout, TimeUnit unit)
awaitTermination
in interface ExecutorService
public <T> Future<T> submit(Callable<T> task)
submit
in interface ExecutorService
public <T> Future<T> submit(Runnable task, T result)
submit
in interface ExecutorService
public Future<?> submit(Runnable task)
submit
in interface ExecutorService
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
invokeAll
in interface ExecutorService
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
invokeAll
in interface ExecutorService
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
invokeAny
in interface ExecutorService
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
invokeAny
in interface ExecutorService
public void triggerAll()
queuedRunnables
.public void trigger()
public int numQueuedRunnables()
public Collection<ScheduledFuture<?>> getScheduledTasks()
public Collection<ScheduledFuture<?>> getPeriodicScheduledTask()
public Collection<ScheduledFuture<?>> getNonPeriodicScheduledTask()
public void triggerScheduledTasks()
public void triggerNonPeriodicScheduledTask()
NoSuchElementException
- If there is no such task.public void triggerNonPeriodicScheduledTasksWithRecursion()
triggerNonPeriodicScheduledTasks()
, if such a task schedules another non-periodically
schedule task, then this new task will also be triggered.public void triggerNonPeriodicScheduledTasks()
public void triggerPeriodicScheduledTasks()
Copyright © 2014–2021 The Apache Software Foundation. All rights reserved.