public abstract class ThreadContext extends Object
An internal HashMap is used to maintain the key/value pairs
for each thread.
If the desired behavior is to ensure that bound data is not shared across threads in a pooled or reusable threaded environment, the application (or more likely a framework) must bind and remove any necessary values at the beginning and end of stack execution, respectively (i.e. individually explicitly or all via the clear method).
remove()| 限定符 | 构造器和说明 |
|---|---|
protected |
ThreadContext()
Default no-argument constructor.
|
| 限定符和类型 | 方法和说明 |
|---|---|
static Object |
get(Object key)
Returns the object for the specified
key that is bound to
the current thread. |
static Map<Object,Object> |
getResources()
Returns the ThreadLocal Map.
|
static void |
put(Object key,
Object value)
Binds value for the given
key to the current thread. |
static void |
remove()
Removes the underlying ThreadLocal from the thread. |
static Object |
remove(Object key)
Unbinds the value for the given
key from the current
thread. |
static void |
setResources(Map<Object,Object> newResources)
Allows a caller to explicitly set the entire resource map.
|
public static Map<Object,Object> getResources()
public static void setResources(Map<Object,Object> newResources)
getResources() method, which will give you the existing state.newResources - the resources to replace the existing resources.public static Object get(Object key)
key that is bound to
the current thread.key - the key that identifies the value to returnkey or null if
no value exists for the specified keypublic static void put(Object key, Object value)
key to the current thread.
A null value has the same effect as if remove was called for the given key, i.e.:
if ( value == null ) {
remove( key );
}key - The key with which to identify the value.value - The value to bind to the thread.IllegalArgumentException - if the key argument is null.public static Object remove(Object key)
key from the current
thread.key - The key identifying the value bound to the current thread.public static void remove()
Removes the underlying ThreadLocal from the thread.
This method is meant to be the final 'clean up' operation that is called at the end of thread execution to prevent thread corruption in pooled thread environments.
Copyright © 2020 nomou. All rights reserved.