Class SimplePeriodicRunnerFactory
- java.lang.Object
-
- com.pervasivecode.utils.time.impl.SimplePeriodicRunnerFactory
-
public class SimplePeriodicRunnerFactory extends Object
Instances of this class will run a singleRunnable
task repeatedly at a rate specified in terms of an interval.Limitations:
- Each instance of
SimplePeriodicRunnerFactory
wraps a single execution thread, so long-running tasks whose duration exceeds the scheduling interval can cause subsequent executions to be late in starting. - Starting multiple concurrent tasks via the same
SimplePeriodicRunnerFactory
(by callinggetRunnerForInterval(java.time.Duration)
multiple times, producing multiple instances ofSimplePeriodicRunner
that share the same thread) can cause similar interactions, where a single task can block all other eligible tasks from running on time.
- Each instance of
-
-
Constructor Summary
Constructors Constructor Description SimplePeriodicRunnerFactory()
Create an instance that will produceSimplePeriodicRunner
instances that all share the same scheduling thread.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SimplePeriodicRunner
getRunnerForInterval(Duration executionInterval)
Get an instance of aSimplePeriodicRunner
that will allow aRunnable
to be executed at the rate specified via the executionInterval.void
shutdownGracefully(long timeout, TimeUnit unit)
Wait up to a specified amount of time for a currently-runningRunnable
task to finish, if any, and then shut down.void
shutdownNow()
Interrupt a running task (if any) and do not run any scheduled tasks from now on.
-
-
-
Constructor Detail
-
SimplePeriodicRunnerFactory
public SimplePeriodicRunnerFactory()
Create an instance that will produceSimplePeriodicRunner
instances that all share the same scheduling thread. (To avoid delays caused by long-running tasks, isolate them by using more than one instance of this factory class, so separate scheduling threads are used to run the tasks.)
-
-
Method Detail
-
getRunnerForInterval
public SimplePeriodicRunner getRunnerForInterval(Duration executionInterval)
Get an instance of aSimplePeriodicRunner
that will allow aRunnable
to be executed at the rate specified via the executionInterval.- Parameters:
executionInterval
- The desired amount of time to delay execution of the provided Runnable task after the previous start of execution of that task.- Returns:
- The instance that will run a task at the specified interval.
-
shutdownGracefully
public void shutdownGracefully(long timeout, TimeUnit unit) throws InterruptedException
Wait up to a specified amount of time for a currently-runningRunnable
task to finish, if any, and then shut down. No more scheduled tasks will be run.- Parameters:
timeout
- How long to wait for currently-running tasks to finish.unit
- The units of the timeout parameter.- Throws:
InterruptedException
- If the calling thread was interrupted while waiting for a currently-running task to finish.
-
shutdownNow
public void shutdownNow()
Interrupt a running task (if any) and do not run any scheduled tasks from now on.
-
-