Interface Scheduler
public interface Scheduler
System component that provides access to standard thread pools used
throughout the kOS system. There are many use cases where code simply needs to
schedule some future work, or needs to run some task in a thread.
Rather than create threads all over the place, just use this scheduler, which
keeps a limit on the overall number of threads in the system.
- Since:
- 1.0
- Version:
- 2022-02-10
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Immediately executes aRunnable
in the worker thread pool.getNamedThreadFactory
(String name) Returns aThreadFactory
that names new threads based on the providedname
.Returns the scheduled thread pool executor.Returns the main thread pool executor.scheduleCallback
(Runnable command, long delay) Schedules aRunnable
that is executeddelay
number of milliseconds in the future.scheduleCallback
(Runnable command, long delay, double min, double max) Schedules aRunnable
to be executed in the future.scheduleCallback
(Runnable command, Date date) Schedules aRunnable
that is executed at the specifiedDate
.scheduleCallbackAtFixedRate
(Runnable command, long initialDelay, long period) Schedules aRunnable
that is executed on a recurring basis; first after the specified initial delay, and then at the given fixed period.scheduleCallbackAtFixedRate
(Runnable command, Date date, long period) Schedules aRunnable
that is executed on a recurring basis; first after the specified date, and then at the fixed period.scheduleCallbackWithFixedDelay
(Runnable command, long initialDelay, long delay) Schedules aRunnable
that is executed on a recurring basis; first after theinitialDelay
, and then with a fixeddelay
between the end of the previous callback and the start of the next.void
shutdown()
Terminates the scheduler.void
sleep
(long msec) Wrapper forThread.sleep()
with default exception handling.startThread
(String name, boolean daemon, Runnable runnable) Executes theRunnable
in a newly created thread.
-
Method Details
-
shutdown
Terminates the scheduler.- Throws:
Exception
- should an error occur
-
startThread
Executes theRunnable
in a newly created thread. This is a convenience method for creating and executing a long-running operation.- Parameters:
name
- the name of the threaddaemon
- true if the thread is a daemon threadrunnable
- theRunnable
to execute- Returns:
- the new running thread
-
execute
Immediately executes aRunnable
in the worker thread pool.- Parameters:
runnable
- theRunnable
to execute
-
scheduleCallback
Schedules aRunnable
that is executeddelay
number of milliseconds in the future. AFuture
is returned, which can be used to cancel the callback.- Parameters:
command
- the command to executedelay
- delay before the command is executed (in msec)- Returns:
Future
for use in cancelling the callback
-
scheduleCallback
Schedules aRunnable
to be executed in the future. The delay is a random number in the specified range using the specifieddelay
value as 100%. This returns aFuture
which can be used to cancel the callback.- Parameters:
command
- the command to executedelay
- delay until the command executes (in msec)min
- the minimum percent of delay (0..1)max
- the maximum percent of delay (1..N)- Returns:
Future
for use in cancelling the callback
-
scheduleCallback
Schedules aRunnable
that is executed at the specifiedDate
. This returns aFuture
which can be used to cancel the callback.- Parameters:
command
- the command to executedate
- when to execute the command- Returns:
Future
for use in cancelling the callback
-
scheduleCallbackAtFixedRate
Schedules aRunnable
that is executed on a recurring basis; first after the specified initial delay, and then at the given fixed period.- Parameters:
command
- the command to executeinitialDelay
- delay until the executing the command the first time (in msec)period
- delay for subsequent executions (in msec)- Returns:
Future
for use in cancelling the callback
-
scheduleCallbackAtFixedRate
Schedules aRunnable
that is executed on a recurring basis; first after the specified date, and then at the fixed period.- Parameters:
command
- the command to executedate
- when to execute the command the first timeperiod
- delay for subsequent executions (in msec)- Returns:
Future
for use in cancelling the callback
-
scheduleCallbackWithFixedDelay
Schedules aRunnable
that is executed on a recurring basis; first after theinitialDelay
, and then with a fixeddelay
between the end of the previous callback and the start of the next.- Parameters:
command
- the command to executeinitialDelay
- delay until the executing the command the first time (in msec)delay
- delay between the end of one callback and the start of the next (in msec)- Returns:
Future
for use in cancelling the callback
-
sleep
void sleep(long msec) Wrapper forThread.sleep()
with default exception handling.- Parameters:
msec
- the number of milliseconds to sleep
-
getWorkerThreadPool
ExecutorService getWorkerThreadPool()Returns the main thread pool executor.- Returns:
- the main thread pool
-
getScheduledThreadPool
ScheduledThreadPoolExecutor getScheduledThreadPool()Returns the scheduled thread pool executor.- Returns:
- the scheduled thread pool
-
getNamedThreadFactory
Returns aThreadFactory
that names new threads based on the providedname
.- Parameters:
name
- the base name for all threads created by the factory
-