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 TypeMethodDescriptionvoidImmediately executes aRunnablein the worker thread pool.getNamedThreadFactory(String name) Returns aThreadFactorythat 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 aRunnablethat is executeddelaynumber of milliseconds in the future.scheduleCallback(Runnable command, long delay, double min, double max) Schedules aRunnableto be executed in the future.scheduleCallback(Runnable command, Date date) Schedules aRunnablethat is executed at the specifiedDate.scheduleCallbackAtFixedRate(Runnable command, long initialDelay, long period) Schedules aRunnablethat 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 aRunnablethat 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 aRunnablethat is executed on a recurring basis; first after theinitialDelay, and then with a fixeddelaybetween the end of the previous callback and the start of the next.voidshutdown()Terminates the scheduler.voidsleep(long msec) Wrapper forThread.sleep()with default exception handling.startThread(String name, boolean daemon, Runnable runnable) Executes theRunnablein a newly created thread.
-
Method Details
-
shutdown
Terminates the scheduler.- Throws:
Exception- should an error occur
-
startThread
Executes theRunnablein 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- theRunnableto execute- Returns:
- the new running thread
-
execute
Immediately executes aRunnablein the worker thread pool.- Parameters:
runnable- theRunnableto execute
-
scheduleCallback
Schedules aRunnablethat is executeddelaynumber of milliseconds in the future. AFutureis returned, which can be used to cancel the callback.- Parameters:
command- the command to executedelay- delay before the command is executed (in msec)- Returns:
Futurefor use in cancelling the callback
-
scheduleCallback
Schedules aRunnableto be executed in the future. The delay is a random number in the specified range using the specifieddelayvalue as 100%. This returns aFuturewhich 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:
Futurefor use in cancelling the callback
-
scheduleCallback
Schedules aRunnablethat is executed at the specifiedDate. This returns aFuturewhich can be used to cancel the callback.- Parameters:
command- the command to executedate- when to execute the command- Returns:
Futurefor use in cancelling the callback
-
scheduleCallbackAtFixedRate
Schedules aRunnablethat 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:
Futurefor use in cancelling the callback
-
scheduleCallbackAtFixedRate
Schedules aRunnablethat 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:
Futurefor use in cancelling the callback
-
scheduleCallbackWithFixedDelay
Schedules aRunnablethat is executed on a recurring basis; first after theinitialDelay, and then with a fixeddelaybetween 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:
Futurefor 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 aThreadFactorythat names new threads based on the providedname.- Parameters:
name- the base name for all threads created by the factory
-