Class ThreadContext
- java.lang.Object
-
- com.im.commons.base.ThreadContext
-
public final class ThreadContext extends java.lang.ObjectThe 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 ThreadContextEMPTYAn emptyThreadContext.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <V> VcallInContext(java.util.concurrent.Callable<V> task, java.util.Map<?,?> context)Calls aCallabletask with the suppliedcontext.static java.lang.RunnablecontextAware(java.lang.Runnable task)Creates aRunnablewrapper that preserves the currentThreadContext.static <V> java.util.concurrent.Callable<V>contextAware(java.util.concurrent.Callable<V> task)Creates aCallablewrapper that preserves the currentThreadContext.static java.util.concurrent.ExecutorServicecontextAware(java.util.concurrent.ExecutorService executors)Creates anExecutorServicewrapper that preservesThreadContext.static ThreadContextgetCurrent()Gets theThreadContextfor the current thread.<T> TgetProperty(java.lang.Object key)Gets a property value for a key stored in thisThreadContext.static voidrunInContext(java.lang.Runnable task, java.util.Map<?,?> context)Runs aRunnabletask with the suppliedcontext.static voidset(ThreadContext value)java.lang.StringtoString()
-
-
-
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 theThreadContextfor the current thread.- Returns:
- Current thread's context or
EMPTYif the current thread has no context.
-
runInContext
public static void runInContext(java.lang.Runnable task, java.util.Map<?,?> context)Runs aRunnabletask 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.ExceptionCalls aCallabletask 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- TheCallabletask 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 aCallablewrapper that preserves the currentThreadContext.This method must be used by any code that schedules
Callabletasks running on a background thread. Failing to do so may result in the scheduler'sThreadContextnot being visible in the spawned background threads. In particular this is important when using thread pools for performing background tasks such as variousExecutorServiceimplementations provided byExecutorsclass.This example shows how to implement a
ThreadContextfriendly 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- TheCallabletask result type.- Parameters:
task- The task to accompany with the currentThreadContext.- Returns:
- The
ThreadContextfriendlyCallablefor the original task.
-
contextAware
public static java.lang.Runnable contextAware(java.lang.Runnable task)
Creates aRunnablewrapper that preserves the currentThreadContext.This method must be used by any code that schedules
Runnabletasks running on a background thread. Failing to do so may result in the scheduler'sThreadContextnot being visible in the spawned background threads. In particular this is important when using thread pools for performing background tasks such as variousExecutorServiceimplementations provided byExecutorsclass.This example shows how to implement a
ThreadContextfriendly 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
ThreadContextfriendlyRunnablefor the original task.
-
contextAware
public static java.util.concurrent.ExecutorService contextAware(java.util.concurrent.ExecutorService executors)
Creates anExecutorServicewrapper that preservesThreadContext.This is an alternative to calling
contextAware(Runnable)andcontextAware(Callable)methods, which can be used for creating aThreadContextawareExecutionService. Any code that creates and usesExecutorServicemust 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'sThreadContextnot being visible in the spawned background threads.This example shows how to create a
ThreadContextfriendlyExecutorService.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
ThreadContextfriendlyExecutorService..
-
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
nullif such a property is not in thisThreadContext.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-