public final class ThreadContext extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
ThreadContext.StoredContext |
| Modifier and Type | Field and Description |
|---|---|
static ThreadContextStack |
EMPTY_STACK |
| Modifier and Type | Method and Description |
|---|---|
static void |
clearAll()
Clears the context map and stack.
|
static void |
clearMap()
Clears the context map.
|
static void |
clearStack()
Clears the stack for this thread.
|
static ThreadContextStack |
cloneStack()
Returns a copy of this thread's stack.
|
static boolean |
containsKey(String key)
Determines if the key is in the context.
|
static String |
get(String key)
Gets the context value identified by the
key parameter. |
static Map<String,String> |
getContext()
Returns a mutable copy of current thread's context Map.
|
static int |
getDepth()
Gets the current nesting depth of this thread's stack.
|
static Map<String,String> |
getImmutableContext()
Returns an immutable view of the current thread's context Map.
|
static ThreadContextStack |
getImmutableStack()
Gets an immutable copy of this current thread's context stack.
|
static ThreadContextMap |
getThreadContextMap() |
static boolean |
isEmpty()
Returns true if the Map is empty.
|
Supplier<ThreadContext.StoredContext> |
newRestorableContext()
Supplier<ThreadContext.StoredContext> restorable = context.newRestorableContext();
new Thread() {
public void run() {
try (ThreadContext.StoredContext ctx = restorable.get()) {
// execute with the parents context and restore the threads context afterwards
}
}
}.start();
|
static String |
peek()
Looks at the last diagnostic context at the top of this NDC without removing it.
|
static String |
pop()
Returns the value of the last item placed on the stack.
|
ThreadContext.StoredContext |
preserveContext()
// current context is stored and replaced with a default context
try (StoredContext context = threadContext.preserveContext()) {
// execute();
}
// previous context is restored on StoredContext#close()
|
static void |
push(String message)
Pushes new diagnostic context information for the current thread.
|
static void |
put(String key,
String value)
Puts a context value (the
value parameter) as identified with the key parameter into
the current thread's context map. |
static void |
putAll(Map<String,String> m)
Puts all given context map entries into the current thread's
context map.
|
static void |
putIfAbsent(String key,
String value)
Puts a context value (the
value parameter) as identified with the key parameter into
the current thread's context map if the key does not exist. |
static void |
remove(String key)
Removes the context value identified by the
key parameter. |
static void |
removeAll(Iterable<String> keys)
Removes the context values identified by the
keys parameter. |
static void |
removeStack()
Removes the diagnostic context for this thread.
|
static void |
setStack(Collection<String> stack)
Sets this thread's stack.
|
ThreadContext.StoredContext |
storeContext() |
static void |
trim(int depth) |
Supplier<ThreadContext.StoredContext> |
wrapRestorable(ThreadContext.StoredContext storedContext) |
public static final ThreadContextStack EMPTY_STACK
public ThreadContext.StoredContext preserveContext()
// current context is stored and replaced with a default context
try (StoredContext context = threadContext.preserveContext()) {
// execute();
}
// previous context is restored on StoredContext#close()
public ThreadContext.StoredContext storeContext()
public Supplier<ThreadContext.StoredContext> newRestorableContext()
Supplier<ThreadContext.StoredContext> restorable = context.newRestorableContext();
new Thread() {
public void run() {
try (ThreadContext.StoredContext ctx = restorable.get()) {
// execute with the parents context and restore the threads context afterwards
}
}
}.start();
public Supplier<ThreadContext.StoredContext> wrapRestorable(ThreadContext.StoredContext storedContext)
public static void put(String key, String value)
value parameter) as identified with the key parameter into
the current thread's context map.
If the current thread does not have a context map it is created as a side effect.
key - The key name.value - The key value.public static void putIfAbsent(String key, String value)
value parameter) as identified with the key parameter into
the current thread's context map if the key does not exist.
If the current thread does not have a context map it is created as a side effect.
key - The key name.value - The key value.public static void putAll(Map<String,String> m)
If the current thread does not have a context map it is created as a side effect.
m - The map.public static String get(String key)
key parameter.
This method has no side effects.
key - The key to locate.public static void remove(String key)
key parameter.key - The key to remove.public static void removeAll(Iterable<String> keys)
keys parameter.keys - The keys to remove.public static void clearMap()
public static void clearAll()
public static boolean containsKey(String key)
key - The key to locate.public static Map<String,String> getContext()
public static Map<String,String> getImmutableContext()
public static ThreadContextMap getThreadContextMap()
public static boolean isEmpty()
public static void clearStack()
public static ThreadContextStack cloneStack()
public static ThreadContextStack getImmutableStack()
public static void setStack(Collection<String> stack)
stack - The stack to use.public static int getDepth()
trim(int)public static String pop()
The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.
public static String peek()
The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.
public static void push(String message)
The contents of the message parameter is determined solely by the client.
message - The new diagnostic context information.public static void removeStack()
Each thread that created a diagnostic context by calling push(java.lang.String) should call this method before exiting.
Otherwise, the memory used by the thread cannot be reclaimed by the VM.
As this is such an important problem in heavy duty systems and because it is difficult to always guarantee that
the remove method is called before exiting a thread, this method has been augmented to lazily remove references
to dead threads. In practice, this means that you can be a little sloppy and occasionally forget to call
remove(java.lang.String) before exiting a thread. However, you must call remove sometime. If you never call
it, then your application is sure to run out of memory.
public static void trim(int depth)
Copyright © 2022. All rights reserved.