Class ThreadContext
- java.lang.Object
-
- com.im.commons.base.ThreadContext
-
public final class ThreadContext extends java.lang.Object
The support for associating properties with threads. This class supports three main usecases.- Context providers - associate an arbitrary number of key-value pairs (properties) with the current thread.
See
runInContext(Runnable, Map)
andcallInContext(Callable, Map)
. - Context consumers - query the current thread's context and retrieve its properties.
See
getCurrent()
. - Context-friendly schedulers - implement thread pools that safely inherit the context from the calling
thread scheduling a task to the background worker thread running the task. See
contextAware(Runnable)
andcontextAware(Callable)
.
- Since:
- 6.4
- Context providers - associate an arbitrary number of key-value pairs (properties) with the current thread.
See
-
-
Field Summary
Fields Modifier and Type Field Description static ThreadContext
EMPTY
An emptyThreadContext
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <V> V
callInContext(java.util.concurrent.Callable<V> task, java.util.Map<?,?> context)
Calls aCallable
task with the suppliedcontext
.static java.lang.Runnable
contextAware(java.lang.Runnable task)
Creates aRunnable
wrapper that preserves the currentThreadContext
.static <V> java.util.concurrent.Callable<V>
contextAware(java.util.concurrent.Callable<V> task)
Creates aCallable
wrapper that preserves the currentThreadContext
.static java.util.concurrent.ExecutorService
contextAware(java.util.concurrent.ExecutorService executors)
Creates anExecutorService
wrapper that preservesThreadContext
.static ThreadContext
getCurrent()
Gets theThreadContext
for the current thread.<T> T
getProperty(java.lang.Object key)
Gets a property value for a key stored in thisThreadContext
.static void
runInContext(java.lang.Runnable task, java.util.Map<?,?> context)
Runs aRunnable
task with the suppliedcontext
.static void
set(ThreadContext value)
java.lang.String
toString()
-
-
-
Field Detail
-
EMPTY
public static final ThreadContext EMPTY
An emptyThreadContext
. This context is returned fromgetCurrent()
if there is no context set for the current thread.
-
-
Method Detail
-
set
public static void set(ThreadContext value)
-
getCurrent
public static ThreadContext getCurrent()
Gets theThreadContext
for the current thread.- Returns:
- Current thread's context or
EMPTY
if the current thread has no context.
-
runInContext
public static void runInContext(java.lang.Runnable task, java.util.Map<?,?> context)
Runs aRunnable
task with the suppliedcontext
. If there is already some context associated with the calling thread it is merged with the new context passed into this method. The new context takes precedence, however.- Parameters:
task
- The task to run.context
- The context to associate with the calling thread. This context is then made available within the task if it callsgetCurrent()
.
-
callInContext
public static <V> V callInContext(java.util.concurrent.Callable<V> task, java.util.Map<?,?> context) throws java.lang.Exception
Calls aCallable
task with the suppliedcontext
. If there is already some context associated with the calling thread it is merged with the new context passed into this method. The new context takes precedence, however.- Type Parameters:
V
- TheCallable
task result type.- Parameters:
task
- The task to run.context
- The context to associate with the calling thread. This context is then made available within the task if it callsgetCurrent()
.- Returns:
- result of the task
- Throws:
java.lang.Exception
- if unable to compute a result
-
contextAware
public static <V> java.util.concurrent.Callable<V> contextAware(java.util.concurrent.Callable<V> task)
Creates aCallable
wrapper that preserves the currentThreadContext
.This method must be used by any code that schedules
Callable
tasks running on a background thread. Failing to do so may result in the scheduler'sThreadContext
not being visible in the spawned background threads. In particular this is important when using thread pools for performing background tasks such as variousExecutorService
implementations provided byExecutors
class.This example shows how to implement a
ThreadContext
friendly scheduler.private final ExecutorService E = Executors.newFixedThreadPool(5); ... public Foo perform() { return E.submit(ThreadContext.contextAware(new Callable<Foo>() { public Foo call() { ... } })); }
- Type Parameters:
V
- TheCallable
task result type.- Parameters:
task
- The task to accompany with the currentThreadContext
.- Returns:
- The
ThreadContext
friendlyCallable
for the original task.
-
contextAware
public static java.lang.Runnable contextAware(java.lang.Runnable task)
Creates aRunnable
wrapper that preserves the currentThreadContext
.This method must be used by any code that schedules
Runnable
tasks running on a background thread. Failing to do so may result in the scheduler'sThreadContext
not being visible in the spawned background threads. In particular this is important when using thread pools for performing background tasks such as variousExecutorService
implementations provided byExecutors
class.This example shows how to implement a
ThreadContext
friendly scheduler.private final ExecutorService E = Executors.newFixedThreadPool(5); ... public void perform() { E.submit(ThreadContext.contextAware(new Runnable() { public void run() { ... } })); }
- Parameters:
task
- The task to accompany with the currentThreadContext
.- Returns:
- The
ThreadContext
friendlyRunnable
for the original task.
-
contextAware
public static java.util.concurrent.ExecutorService contextAware(java.util.concurrent.ExecutorService executors)
Creates anExecutorService
wrapper that preservesThreadContext
.This is an alternative to calling
contextAware(Runnable)
andcontextAware(Callable)
methods, which can be used for creating aThreadContext
awareExecutionService
. Any code that creates and usesExecutorService
must either use this method or take care of usingcontextAware(Runnable)
orcontextAware(Callable)
for particular tasks that it schedules. Failing to do so may result in the scheduler'sThreadContext
not being visible in the spawned background threads.This example shows how to create a
ThreadContext
friendlyExecutorService
.private final ExecutorService E = ThreadContext.contextAware(Executors.newFixedThreadPool(5)); ... public void perform() { E.submit(new Runnable() { public void run() { ... } }); }
- Parameters:
executors
- The originalExecutorService
.- Returns:
- The
ThreadContext
friendlyExecutorService
..
-
getProperty
public <T> T getProperty(java.lang.Object key)
Gets a property value for a key stored in thisThreadContext
.- Type Parameters:
T
- The property value type.- Parameters:
key
- The property key.- Returns:
- The property value or
null
if such a property is not in thisThreadContext
.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-