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 Details

    • shutdown

      void shutdown() throws Exception
      Terminates the scheduler.
      Throws:
      Exception - should an error occur
    • startThread

      Thread startThread(String name, boolean daemon, Runnable runnable)
      Executes the Runnable in a newly created thread. This is a convenience method for creating and executing a long-running operation.
      Parameters:
      name - the name of the thread
      daemon - true if the thread is a daemon thread
      runnable - the Runnable to execute
      Returns:
      the new running thread
    • execute

      void execute(Runnable runnable)
      Immediately executes a Runnable in the worker thread pool.
      Parameters:
      runnable - the Runnable to execute
    • scheduleCallback

      ScheduledFuture<?> scheduleCallback(Runnable command, long delay)
      Schedules a Runnable that is executed delay number of milliseconds in the future. A Future is returned, which can be used to cancel the callback.
      Parameters:
      command - the command to execute
      delay - delay before the command is executed (in msec)
      Returns:
      Future for use in cancelling the callback
    • scheduleCallback

      ScheduledFuture<?> scheduleCallback(Runnable command, long delay, double min, double max)
      Schedules a Runnable to be executed in the future. The delay is a random number in the specified range using the specified delay value as 100%. This returns a Future which can be used to cancel the callback.
      Parameters:
      command - the command to execute
      delay - 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

      ScheduledFuture<?> scheduleCallback(Runnable command, Date date)
      Schedules a Runnable that is executed at the specified Date . This returns a Future which can be used to cancel the callback.
      Parameters:
      command - the command to execute
      date - when to execute the command
      Returns:
      Future for use in cancelling the callback
    • scheduleCallbackAtFixedRate

      ScheduledFuture<?> scheduleCallbackAtFixedRate(Runnable command, long initialDelay, long period)
      Schedules a Runnable 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 execute
      initialDelay - 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

      ScheduledFuture<?> scheduleCallbackAtFixedRate(Runnable command, Date date, long period)
      Schedules a Runnable that is executed on a recurring basis; first after the specified date, and then at the fixed period.
      Parameters:
      command - the command to execute
      date - when to execute the command the first time
      period - delay for subsequent executions (in msec)
      Returns:
      Future for use in cancelling the callback
    • scheduleCallbackWithFixedDelay

      ScheduledFuture<?> scheduleCallbackWithFixedDelay(Runnable command, long initialDelay, long delay)
      Schedules a Runnable that is executed on a recurring basis; first after the initialDelay , and then with a fixed delay between the end of the previous callback and the start of the next.
      Parameters:
      command - the command to execute
      initialDelay - 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 for Thread.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

      ThreadFactory getNamedThreadFactory(String name)
      Returns a ThreadFactory that names new threads based on the provided name .
      Parameters:
      name - the base name for all threads created by the factory