Package io.atomix.utils.concurrent
Class SingleThreadContext
- java.lang.Object
-
- io.atomix.utils.concurrent.AbstractThreadContext
-
- io.atomix.utils.concurrent.SingleThreadContext
-
- All Implemented Interfaces:
Scheduler,ThreadContext,AutoCloseable,Executor
- Direct Known Subclasses:
BlockingAwareSingleThreadContext
public class SingleThreadContext extends AbstractThreadContext
Single threaded context.This is a basic
ThreadContextimplementation that uses aScheduledExecutorServiceto schedule events on the context thread.- Author:
- Jordan Halterman
-
-
Field Summary
Fields Modifier and Type Field Description protected ScheduledExecutorServiceexecutorprotected static org.slf4j.LoggerLOGGER
-
Constructor Summary
Constructors Modifier Constructor Description SingleThreadContext(String nameFormat)Creates a new single thread context.SingleThreadContext(String nameFormat, Consumer<Throwable> uncaughtExceptionObserver)Creates a new single thread context.protectedSingleThreadContext(ScheduledExecutorService executor, Consumer<Throwable> uncaughtExceptionObserver)Creates a new single thread context.SingleThreadContext(ThreadFactory factory)Creates a new single thread context.SingleThreadContext(ThreadFactory factory, Consumer<Throwable> uncaughtExceptionObserver)Creates a new single thread context.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the context.voidexecute(Runnable command)protected static AtomixThreadgetThread(ExecutorService executor)Gets the thread from a single threaded executor service.Scheduledschedule(Duration delay, Runnable runnable)Schedules a runnable after a delay.Scheduledschedule(Duration delay, Duration interval, Runnable runnable)Schedules a runnable at a fixed rate.-
Methods inherited from class io.atomix.utils.concurrent.AbstractThreadContext
block, isBlocked, unblock
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.atomix.utils.concurrent.ThreadContext
checkThread, isCurrentContext
-
-
-
-
Field Detail
-
LOGGER
protected static final org.slf4j.Logger LOGGER
-
executor
protected final ScheduledExecutorService executor
-
-
Constructor Detail
-
SingleThreadContext
public SingleThreadContext(String nameFormat)
Creates a new single thread context.The provided context name will be passed to
AtomixThreadFactoryand used when instantiating the context thread.- Parameters:
nameFormat- The context nameFormat which will be formatted with a thread number.
-
SingleThreadContext
public SingleThreadContext(String nameFormat, Consumer<Throwable> uncaughtExceptionObserver)
Creates a new single thread context.The provided context name will be passed to
AtomixThreadFactoryand used when instantiating the context thread.- Parameters:
nameFormat- The context nameFormat which will be formatted with a thread number.uncaughtExceptionObserver- A consumer to observe exceptions thrown by submitted tasks
-
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 Detail
-
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:SchedulerSchedules a runnable after a delay.- Parameters:
delay- the delay after which to run the callbackrunnable- the callback to run- Returns:
- the scheduled callback
-
schedule
public Scheduled schedule(Duration delay, Duration interval, Runnable runnable)
Description copied from interface:SchedulerSchedules a runnable at a fixed rate.- Parameters:
delay- the initial delayinterval- the interval at which to run the callbackrunnable- the callback to run- Returns:
- the scheduled callback
-
close
public void close()
Description copied from interface:ThreadContextCloses the context.
-
-