Improves support of log4j2.enableThreadlocals
property
#2550
+420
−95
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If the
log4j2.enableThreadlocals
property is set tofalse
Log4j should clear allThreadLocal
s from the calling thread before execution finishes.The same rule should apply to user methods that correctly use the
ThreadLocal
interface. Code like this:should perform a call to
ThreadLocal#remove
before exiting.While the difference between
threadLocal.remove()
andthreadLocal.set(null)
is almost imperceptible in a classical threading model, when virtual threads are used the difference might be considerable.This PR:
ThreadContextMap
andThreadContextStack
implementations (exceptGarbageFreeSortedArrayThreadContextMap
) so that the context map/stack is cleared if the map/stack is empty.ThreadLocal
s used to detect recursion to callThreadLocal#remove
when the recursion counter reaches 0.ThreadLocal
used for object pooling to respect thelog4j2.enableThreadlocals
property.Remark: This PR does not address LOG4J-2753 that will be fixed in another PR.
Part of #2525.