Class Pool<T>
- Type Parameters:
T- the type of the pooled objects
- All Implemented Interfaces:
AutoCloseable,Dumpable
A pool of objects, with optional support for multiplexing,
max usage count and several optimized strategies plus
an optional ThreadLocal cache of the last release entry.
When the method close() is called, all Closeables
object pooled by the pool are also closed.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassA Pool entry that holds metadata and a pooled object.static enumThe type of the strategy to use for the pool.Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer -
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionPool(Pool.StrategyType strategyType, int maxEntries) Construct a Pool with a specified lookup strategy and noThreadLocalcache.Pool(Pool.StrategyType strategyType, int maxEntries, boolean cache) Construct a Pool with the specified thread-local cache size and an optionalThreadLocalcache. -
Method Summary
Modifier and TypeMethodDescriptionacquire()Acquires an entry from the pool.Acquires an entry from the pool, reserving and creating a new entry if necessary.voidclose()voiddump(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.intintintintintDeprecated.Multiplex functionalities will be removedprotected intgetMaxMultiplex(T pooled) Deprecated.Multiplex functionalities will be removedintDeprecated.MaxUsage functionalities will be removedprotected intgetMaxUsageCount(T pooled) Deprecated.MaxUsage functionalities will be removedintbooleanisClosed()booleanReleases anacquiredentry to the pool.booleanRemoves an entry from the pool.reserve()Creates a new disabled slot into the pool.reserve(int allotment) Deprecated.Usereserve()insteadfinal voidsetMaxMultiplex(int maxMultiplex) Deprecated.Multiplex functionalities will be removedfinal voidsetMaxUsageCount(int maxUsageCount) Deprecated.MaxUsage functionalities will be removedintsize()toString()Collection<Pool<T>.Entry>values()
-
Constructor Details
-
Pool
Construct a Pool with a specified lookup strategy and noThreadLocalcache.- Parameters:
strategyType- The strategy to used for looking up entries.maxEntries- the maximum amount of entries that the pool will accept.
-
Pool
Construct a Pool with the specified thread-local cache size and an optionalThreadLocalcache.- Parameters:
strategyType- The strategy to used for looking up entries.maxEntries- the maximum amount of entries that the pool will accept.cache- True if aThreadLocalcache should be used to try the most recently released entry.
-
-
Method Details
-
getReservedCount
- Returns:
- the number of reserved entries
-
getIdleCount
- Returns:
- the number of idle entries
-
getInUseCount
- Returns:
- the number of in-use entries
-
getClosedCount
- Returns:
- the number of closed entries
-
getMaxEntries
- Returns:
- the maximum number of entries
-
getMaxMultiplex
@ManagedAttribute("The default maximum multiplex count of entries") @Deprecated public int getMaxMultiplex()Deprecated.Multiplex functionalities will be removed- Returns:
- the default maximum multiplex count of entries
-
getMaxMultiplex
Deprecated.Multiplex functionalities will be removedRetrieves the max multiplex count for the given pooled object.
- Parameters:
pooled- the pooled object- Returns:
- the max multiplex count for the given pooled object
-
setMaxMultiplex
Deprecated.Multiplex functionalities will be removedSets the default maximum multiplex count for the Pool's entries.
- Parameters:
maxMultiplex- the default maximum multiplex count of entries
-
getMaxUsageCount
@ManagedAttribute("The default maximum usage count of entries") @Deprecated public int getMaxUsageCount()Deprecated.MaxUsage functionalities will be removedReturns the maximum number of times the entries of the pool can be acquired.
- Returns:
- the default maximum usage count of entries
-
getMaxUsageCount
Deprecated.MaxUsage functionalities will be removedRetrieves the max usage count for the given pooled object.
- Parameters:
pooled- the pooled object- Returns:
- the max usage count for the given pooled object
-
setMaxUsageCount
Deprecated.MaxUsage functionalities will be removedSets the maximum usage count for the Pool's entries.
All existing idle entries that have a usage count larger than this new value are removed from the Pool and closed.
- Parameters:
maxUsageCount- the default maximum usage count of entries
-
reserve
Deprecated.Usereserve()insteadCreates a new disabled slot into the pool.
The returned entry must ultimately have the
Pool.Entry.enable(Object, boolean)method called or be removed viaPool.Entry.remove()orremove(Pool.Entry).- Parameters:
allotment- the desired allotment, where each entry handles an allotment of maxMultiplex, or a negative number to always trigger the reservation of a new entry.- Returns:
- a disabled entry that is contained in the pool,
or null if the pool is closed or if the pool already contains
getMaxEntries()entries, or the allotment has already been reserved
-
reserve
Creates a new disabled slot into the pool.
The returned entry must ultimately have the
Pool.Entry.enable(Object, boolean)method called or be removed viaPool.Entry.remove()orremove(Pool.Entry).- Returns:
- a disabled entry that is contained in the pool,
or null if the pool is closed or if the pool already contains
getMaxEntries()entries
-
acquire
Acquires an entry from the pool.
Only enabled entries will be returned from this method and their
Pool.Entry.enable(Object, boolean)method must not be called.- Returns:
- an entry from the pool or null if none is available.
-
acquire
Acquires an entry from the pool, reserving and creating a new entry if necessary.
- Parameters:
creator- a function to create the pooled value for a reserved entry.- Returns:
- an entry from the pool or null if none is available.
-
release
Releases an
acquiredentry to the pool.Entries that are acquired from the pool but never released will result in a memory leak.
- Parameters:
entry- the value to return to the pool- Returns:
- true if the entry was released and could be acquired again,
false if the entry should be removed by calling
remove(Pool.Entry)and the object contained by the entry should be disposed.
-
remove
Removes an entry from the pool.
- Parameters:
entry- the value to remove- Returns:
- true if the entry was removed, false otherwise
-
isClosed
public boolean isClosed() -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
size
public int size() -
values
-
dump
Description copied from interface:DumpableDump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.- Specified by:
dumpin interfaceDumpable- Parameters:
out- The appendable to dump toindent- The indent to apply after any new lines.- Throws:
IOException- if unable to write to Appendable
-
toString
-