Class CopyOnWriteSortedArrayThreadContextMap
- All Implemented Interfaces:
CleanableThreadContextMap,CopyOnWrite,ObjectThreadContextMap,ReadOnlyThreadContextMap,ThreadContextMap,ThreadContextMap2
SortedArrayStringMap-based implementation of the ThreadContextMap interface that creates a copy of
the data structure on every modification. Any particular instance of the data structure is a snapshot of the
ThreadContext at some point in time and can safely be passed off to other threads. Since it is
expected that the Map will be passed to many more log events than the number of keys it contains the performance
should be much better than if the Map was copied for each event.- Since:
- 2.7
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intThe default initial capacity.private static final StringMapstatic final StringProperty name ("isThreadContextMapInheritable" ) for selectingInheritableThreadLocal(value "true") or plainThreadLocal(value is not "true") in the implementation.private static booleanprivate static intprivate final ThreadLocal<StringMap> protected static final StringSystem property name that can be used to control the data structure's initial capacity. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the context.booleancontainsKey(String key) Determines if the key is in the context.protected StringMapReturns an implementation of theStringMapused to back this thread context map.protected StringMapcreateStringMap(ReadOnlyStringMap original) Returns an implementation of theStringMapused to back this thread context map, pre-populated with the contents of the specified context data.private ThreadLocal<StringMap> booleanGets the context identified by thekeyparameter.getCopy()Gets a non-nullmutable copy of current thread's context Map.Returns an immutable view on the context Map ornullif the context map is empty.Returns the context data for reading.<V> VReturns the Object value for the specified key, ornullif the specified key does not exist in this collection.inthashCode()(package private) static voidinit()Initializes static variables based on system properties.booleanisEmpty()Returns true if the Map is empty.voidPuts a context value (theoparameter) as identified with thekeyparameter into the current thread's context map.voidPuts all given context map entries into the current thread's context map.<V> voidputAllValues(Map<String, V> values) Puts all given key-value pairs into the collection.voidPuts the specified key-value pair into the collection.voidRemoves the context identified by thekeyparameter.voidRemoves all given context map keys from the current thread's context map.toString()
-
Field Details
-
INHERITABLE_MAP
Property name ("isThreadContextMapInheritable" ) for selectingInheritableThreadLocal(value "true") or plainThreadLocal(value is not "true") in the implementation.- See Also:
-
DEFAULT_INITIAL_CAPACITY
protected static final int DEFAULT_INITIAL_CAPACITYThe default initial capacity.- See Also:
-
PROPERTY_NAME_INITIAL_CAPACITY
System property name that can be used to control the data structure's initial capacity.- See Also:
-
EMPTY_CONTEXT_DATA
-
initialCapacity
private static volatile int initialCapacity -
inheritableMap
private static volatile boolean inheritableMap -
localMap
-
-
Constructor Details
-
CopyOnWriteSortedArrayThreadContextMap
public CopyOnWriteSortedArrayThreadContextMap()
-
-
Method Details
-
init
static void init()Initializes static variables based on system properties. Normally called when this class is initialized by the VM and when Log4j is reconfigured. -
createThreadLocalMap
-
createStringMap
Returns an implementation of theStringMapused to back this thread context map.Subclasses may override.
- Returns:
- an implementation of the
StringMapused to back this thread context map
-
createStringMap
Returns an implementation of theStringMapused to back this thread context map, pre-populated with the contents of the specified context data.Subclasses may override.
- Parameters:
original- the key-value pairs to initialize the returned context data with- Returns:
- an implementation of the
StringMapused to back this thread context map
-
put
Description copied from interface:ThreadContextMapPuts a context value (theoparameter) as identified with thekeyparameter into the current thread's context map.If the current thread does not have a context map it is created as a side effect.
- Specified by:
putin interfaceThreadContextMap- Parameters:
key- The key name.value- The key value.
-
putValue
Description copied from interface:ObjectThreadContextMapPuts the specified key-value pair into the collection.- Specified by:
putValuein interfaceObjectThreadContextMap- Parameters:
key- the key to add or remove. Keys may benull.value- the value to add. Values may benull.
-
putAll
Description copied from interface:ThreadContextMap2Puts all given context map entries into the current thread's context map.If the current thread does not have a context map it is created as a side effect.
- Specified by:
putAllin interfaceThreadContextMap2- Parameters:
values- The map.
-
putAllValues
Description copied from interface:ObjectThreadContextMapPuts all given key-value pairs into the collection.- Specified by:
putAllValuesin interfaceObjectThreadContextMap- Parameters:
values- the map of key-value pairs to add
-
get
Description copied from interface:ReadOnlyThreadContextMapGets the context identified by thekeyparameter.This method has no side effects.
- Specified by:
getin interfaceReadOnlyThreadContextMap- Specified by:
getin interfaceThreadContextMap- Parameters:
key- The key to locate.- Returns:
- The value associated with the key or null.
-
getValue
Description copied from interface:ObjectThreadContextMapReturns the Object value for the specified key, ornullif the specified key does not exist in this collection.- Specified by:
getValuein interfaceObjectThreadContextMap- Parameters:
key- the key whose value to return- Returns:
- the value for the specified key or
null
-
remove
Description copied from interface:ThreadContextMapRemoves the context identified by thekeyparameter.- Specified by:
removein interfaceThreadContextMap- Parameters:
key- The key to remove.
-
removeAll
Description copied from interface:CleanableThreadContextMapRemoves all given context map keys from the current thread's context map.If the current thread does not have a context map it is created as a side effect.
- Specified by:
removeAllin interfaceCleanableThreadContextMap- Parameters:
keys- The keys.
-
clear
public void clear()Description copied from interface:ReadOnlyThreadContextMapClears the context.- Specified by:
clearin interfaceReadOnlyThreadContextMap- Specified by:
clearin interfaceThreadContextMap
-
containsKey
Description copied from interface:ReadOnlyThreadContextMapDetermines if the key is in the context.- Specified by:
containsKeyin interfaceReadOnlyThreadContextMap- Specified by:
containsKeyin interfaceThreadContextMap- Parameters:
key- The key to locate.- Returns:
- True if the key is in the context, false otherwise.
-
getCopy
Description copied from interface:ReadOnlyThreadContextMapGets a non-nullmutable copy of current thread's context Map.- Specified by:
getCopyin interfaceReadOnlyThreadContextMap- Specified by:
getCopyin interfaceThreadContextMap- Returns:
- a mutable copy of the context.
-
getReadOnlyContextData
Returns the context data for reading. Note that regardless of whether the returned context data has been frozen (made read-only) or not, callers should not attempt to modify the returned data structure.Thread safety note:
If this
ReadOnlyThreadContextMapimplementsCopyOnWrite, then the returnedStringMapcan safely be passed to another thread: future changes in the underlying context data will not be reflected in the returnedStringMap.Otherwise, if this
ReadOnlyThreadContextMapdoes not implementCopyOnWrite, then it is not safe to pass the returnedStringMapto another thread because changes in the underlying context may be reflected in the returned object. It is the responsibility of the caller to make a copy to pass to another thread.- Specified by:
getReadOnlyContextDatain interfaceReadOnlyThreadContextMap- Specified by:
getReadOnlyContextDatain interfaceThreadContextMap2- Returns:
- a
StringMapcontaining context data key-value pairs
-
getImmutableMapOrNull
Description copied from interface:ReadOnlyThreadContextMapReturns an immutable view on the context Map ornullif the context map is empty.- Specified by:
getImmutableMapOrNullin interfaceReadOnlyThreadContextMap- Specified by:
getImmutableMapOrNullin interfaceThreadContextMap- Returns:
- an immutable context Map or
null.
-
isEmpty
public boolean isEmpty()Description copied from interface:ReadOnlyThreadContextMapReturns true if the Map is empty.- Specified by:
isEmptyin interfaceReadOnlyThreadContextMap- Specified by:
isEmptyin interfaceThreadContextMap- Returns:
- true if the Map is empty, false otherwise.
-
toString
-
hashCode
public int hashCode() -
equals
-