Package io.github.jbellis.jvector.util
Class PoolingSupport<T>
java.lang.Object
io.github.jbellis.jvector.util.PoolingSupport<T>
- Type Parameters:
T- The object to be pooled by this instance
Allows any object to be pooled and released when work is done.
This is an alternative to using
ThreadLocal.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classWrapper class for items in the pool These are AutoClosable and are intended to be used in a try-with-resources statement. -
Method Summary
Modifier and TypeMethodDescriptionabstract PoolingSupport.Pooled<T> get()static <T> PoolingSupport<T> newNoPooling(T fixedValue) Special case of not actually needing a pool (when other times you do)static <T> PoolingSupport<T> newQueuePooling(int limit, Supplier<T> initialValue) Recycling of objects using a MPMC queuestatic <T> PoolingSupport<T> newThreadBased(Supplier<T> initialValue) Creates a pool of objects intended to be used by a thread pool.protected abstract voidonClosed(PoolingSupport.Pooled<T> value) Internal call used when pooled item is returnedstream()This call returns all values what are in the pool, for the case of work spread across many pooled objects then processed after they are finished.
-
Method Details
-
newThreadBased
Creates a pool of objects intended to be used by a thread pool. This is a replacement for ThreadLocal.- Parameters:
initialValue- allows creation of new instances for the pool
-
newNoPooling
Special case of not actually needing a pool (when other times you do)- Parameters:
fixedValue- the value this pool will always return
-
newQueuePooling
Recycling of objects using a MPMC queue- Parameters:
limit- the specific number of threads to be sharing the pooled objectsinitialValue- allows creation of new instances for the pool
-
get
- Returns:
- a pooled object which will be returned to the pool when thread is finished with it
-
stream
This call returns all values what are in the pool, for the case of work spread across many pooled objects then processed after they are finished.- Returns:
- a stream of everything in the pool.
- Throws:
IllegalStateException- if outstanding items are not yet returned to the pool
-
onClosed
Internal call used when pooled item is returned- Parameters:
value-
-