public class ScopedThreadLocal<T> extends Object implements ScopedResourcePool<T>
ScopedResource instances.
Every thread owns its own stack of resources supplied by supplier. An
instance is taken from the stack when get() is called and returned to
it when the scope closes. If the stack is full the newest resource is
discarded.
The stack is confined to a single thread and so is not synchronised. The resources themselves may not be thread-safe and must only be used by one thread at a time unless they implement their own safety rules.
| Constructor and Description |
|---|
ScopedThreadLocal(@NotNull Supplier<T> supplier,
@NotNull Consumer<T> onAcquire,
int maxInstances)
Creates a pool with an action run on every acquisition.
|
ScopedThreadLocal(@NotNull Supplier<T> supplier,
@NotNull Consumer<T> onAcquire,
int maxInstances,
boolean useWeakReferences)
Creates a pool with fine-grained control over resource retention.
|
ScopedThreadLocal(Supplier<T> supplier,
int maxInstances)
Creates a pool with the given supplier and capacity.
|
| Modifier and Type | Method and Description |
|---|---|
ScopedResource<T> |
get()
Obtains a scoped handle to a pooled instance.
|
public ScopedThreadLocal(Supplier<T> supplier, int maxInstances)
supplier - provides new instances when the stack is emptymaxInstances - maximum number of retained instances per threadpublic ScopedThreadLocal(@NotNull
@NotNull Supplier<T> supplier,
@NotNull
@NotNull Consumer<T> onAcquire,
int maxInstances)
supplier - provides new instances when the stack is emptyonAcquire - invoked each time an instance is retrieved from the poolmaxInstances - maximum number of retained instances per threadpublic ScopedThreadLocal(@NotNull
@NotNull Supplier<T> supplier,
@NotNull
@NotNull Consumer<T> onAcquire,
int maxInstances,
boolean useWeakReferences)
supplier - provides new instances when the stack is emptyonAcquire - invoked each time an instance is retrieved from the poolmaxInstances - maximum number of retained instances per threaduseWeakReferences - if true weak references allow garbage collectionpublic ScopedResource<T> get()
get in interface ScopedResourcePool<T>Copyright © 2026 Chronicle Software Ltd. All rights reserved.