Class SingleThreadContext

java.lang.Object
io.atomix.utils.concurrent.AbstractThreadContext
io.atomix.utils.concurrent.SingleThreadContext
All Implemented Interfaces:
Scheduler, ThreadContext, AutoCloseable, Executor

public class SingleThreadContext extends AbstractThreadContext
Single threaded context.

This is a basic ThreadContext implementation that uses a ScheduledExecutorService to schedule events on the context thread.

Author:
Jordan Halterman
  • Field Details

  • Constructor Details

    • SingleThreadContext

      public SingleThreadContext(String nameFormat)
      Creates a new single thread context.

      The provided context name will be passed to AtomixThreadFactory and used when instantiating the context thread.

      Parameters:
      nameFormat - The context nameFormat which will be formatted with a thread number.
    • SingleThreadContext

      public SingleThreadContext(ThreadFactory factory)
      Creates a new single thread context.
      Parameters:
      factory - The thread factory.
    • SingleThreadContext

      public SingleThreadContext(ThreadFactory factory, Consumer<Throwable> uncaughtExceptionObserver)
      Creates a new single thread context.
      Parameters:
      factory - The thread factory.
      uncaughtExceptionObserver - A consumer to observe exceptions thrown by submitted tasks.
    • SingleThreadContext

      protected SingleThreadContext(ScheduledExecutorService executor, Consumer<Throwable> uncaughtExceptionObserver)
      Creates a new single thread context.
      Parameters:
      executor - The executor on which to schedule events. This must be a single thread scheduled executor.
      uncaughtExceptionObserver - A consumer to observe exceptions thrown by submitted tasks.
  • Method Details

    • getThread

      protected static AtomixThread getThread(ExecutorService executor)
      Gets the thread from a single threaded executor service.
    • execute

      public void execute(Runnable command)
    • schedule

      public Scheduled schedule(Duration delay, Runnable runnable)
      Description copied from interface: Scheduler
      Schedules a runnable after a delay.
      Parameters:
      delay - the delay after which to run the callback
      runnable - the callback to run
      Returns:
      the scheduled callback
    • schedule

      public Scheduled schedule(Duration delay, Duration interval, Runnable runnable)
      Description copied from interface: Scheduler
      Schedules a runnable at a fixed rate.
      Parameters:
      delay - the initial delay
      interval - the interval at which to run the callback
      runnable - the callback to run
      Returns:
      the scheduled callback
    • close

      public void close()
      Description copied from interface: ThreadContext
      Closes the context.