Use a ThreadLocal approach instead of an ObjectPool #670
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.
This PR uses a ThreadLocal approach instead of an ObjectPool to provide a thread-safe value to requesting threads.
The ThreadLocalHolder class maintains a per-thread value using an internal ThreadLocal variable. Values are obtained from the Supplier given to the constructor at initialisation time.
It also maintains a WeakReference to each requesting thread and record the value given to it. As opposed to a standard ThreadLocal, the holder automatically dispose the value when the owning thread dies and sets its reference to null to make it eligible for garbage collection. This process hooks into the #release() method and is therefore executed every time a thread releases its value.
Finally, all allocated values are disposed when the ThreadLocalHolder is closed.
Related to issue #630.