public class ScopedThreadLocal<T> extends Object implements ScopedResourcePool<T>
ScopedResourcePool.
This is used for small, tightly-scoped thread-local resource "pools", a safer alternative to a thread-local singleton.
Holds a limited-depth stack of instances local to each thread, which are allocated as acquired and returned to the stack as the scopes close. If too many are acquired, a warning is logged and the extra instances are made eligible for garbage collection.
| Constructor and Description |
|---|
ScopedThreadLocal(@NotNull Supplier<T> supplier,
@NotNull Consumer<T> onAcquire,
int maxInstances)
Constructor
|
ScopedThreadLocal(@NotNull Supplier<T> supplier,
@NotNull Consumer<T> onAcquire,
int maxInstances,
boolean useWeakReferences)
Constructor
|
ScopedThreadLocal(Supplier<T> supplier,
int maxInstances)
Constructor
|
| Modifier and Type | Method and Description |
|---|---|
ScopedResource<T> |
get()
Get a scoped instance of the shared resource
|
public ScopedThreadLocal(Supplier<T> supplier, int maxInstances)
supplier - The supplier of new instancesmaxInstances - The maximum number of instances that will be retained for re-usepublic ScopedThreadLocal(@NotNull
@NotNull Supplier<T> supplier,
@NotNull
@NotNull Consumer<T> onAcquire,
int maxInstances)
supplier - The supplier of new instancesonAcquire - A function to run on each instance upon it's acquisitionmaxInstances - The maximum number of instances that will be retained for re-usepublic ScopedThreadLocal(@NotNull
@NotNull Supplier<T> supplier,
@NotNull
@NotNull Consumer<T> onAcquire,
int maxInstances,
boolean useWeakReferences)
supplier - The supplier of new instancesonAcquire - A function to run on each instance upon it's acquisitionmaxInstances - The maximum number of instances that will be retained for re-useuseWeakReferences - Whether to allow resources to be garbage collected when they're not in usepublic ScopedResource<T> get()
get in interface ScopedResourcePool<T>ScopedResource, to be closed once it is finished being usedCopyright © 2024. All rights reserved.