Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a ThreadLocal approach instead of an ObjectPool #670

Merged
merged 4 commits into from
Oct 12, 2021

Conversation

brenuart
Copy link
Collaborator

@brenuart brenuart commented Oct 7, 2021

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.

@brenuart brenuart requested a review from philsttr October 7, 2021 23:08
@brenuart brenuart force-pushed the threadlocal-instead-of-ObjectPool branch from 71823ab to feefdb4 Compare October 7, 2021 23:16
Track threads and allocated values.
Dispose value when a thread dies.
Dispose allocated values on close()
@brenuart brenuart force-pushed the threadlocal-instead-of-ObjectPool branch from feefdb4 to 6d53115 Compare October 7, 2021 23:21
@brenuart brenuart force-pushed the threadlocal-instead-of-ObjectPool branch from 1955c40 to 1fce722 Compare October 8, 2021 12:02
Copy link
Collaborator

@philsttr philsttr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Only a couple tiny comments.

@brenuart brenuart merged commit 8995771 into main Oct 12, 2021
@brenuart brenuart deleted the threadlocal-instead-of-ObjectPool branch October 12, 2021 08:11
@philsttr philsttr added this to the 7.0 milestone Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants