public interface ThreadContext extends AutoCloseable
The thread context is used by Catalyst to determine the correct thread on which to execute asynchronous callbacks.
All threads created within Catalyst must be instances of CatalystThread. Once
a thread has been created, the context is stored in the thread object via
CatalystThread.setContext(ThreadContext). This means there is a one-to-one relationship
between a context and a thread. That is, a context is representative of a thread and provides an interface for firing
events on that thread.
In addition to serving as an Executor, the context also provides thread-local storage
for Serializer serializer instances. All serialization that takes place within a
CatalystThread should use the context serializer().
Components of the framework that provide custom threads should use CatalystThreadFactory
to allocate new threads and provide a custom ThreadContext implementation.
| Modifier and Type | Method and Description |
|---|---|
void |
block()
Sets the context state to blocked.
|
default void |
checkThread()
Checks that the current thread is the correct context thread.
|
void |
close()
Closes the context.
|
static ThreadContext |
currentContext()
Returns the current thread context.
|
static ThreadContext |
currentContextOrThrow() |
default CompletableFuture<Void> |
execute(Runnable callback)
Executes a callback on the context.
|
default <T> CompletableFuture<T> |
execute(Supplier<T> callback)
Executes a callback on the context.
|
Executor |
executor()
Returns the underlying executor.
|
boolean |
isBlocked()
Returns a boolean indicating whether the context state is blocked.
|
default boolean |
isCurrentContext()
Returns a boolean indicating whether the current thread is in this context.
|
org.slf4j.Logger |
logger()
Returns the context logger.
|
Scheduled |
schedule(Duration initialDelay,
Duration interval,
Runnable callback)
Schedules a runnable at a fixed rate on the context.
|
Scheduled |
schedule(Duration delay,
Runnable callback)
Schedules a runnable on the context.
|
Serializer |
serializer()
Returns the context serializer.
|
void |
unblock()
Sets the context state to unblocked.
|
static ThreadContext currentContext()
null if no context exists.static ThreadContext currentContextOrThrow()
IllegalStateException - if the current thread is not a catalyst threaddefault boolean isCurrentContext()
default void checkThread()
org.slf4j.Logger logger()
Serializer serializer()
Executor executor()
boolean isBlocked()
void block()
void unblock()
default CompletableFuture<Void> execute(Runnable callback)
callback - The callback to execute.default <T> CompletableFuture<T> execute(Supplier<T> callback)
T - The callback result type.callback - The callback to execute.Scheduled schedule(Duration delay, Runnable callback)
callback - The callback to schedule.delay - The delay at which to schedule the runnable.Scheduled schedule(Duration initialDelay, Duration interval, Runnable callback)
callback - The callback to schedule.void close()
close in interface AutoCloseableCopyright © 2013–2016. All rights reserved.