Class Pool<T>
java.lang.Object
org.eclipse.jetty.util.Pool<T>
- Type Parameters:
T-
- All Implemented Interfaces:
AutoCloseable, Dumpable
A fast 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 in the pool
are also closed.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassstatic enumThe type of the strategy to use for the pool.Nested classes/interfaces inherited from interface Dumpable
Dumpable.DumpableContainer -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ThreadLocal<Pool<T>.Entry> private booleanprivate final Lockerprivate static final Loggerprivate final intprivate intprivate intprivate final AtomicIntegerprivate final Pool.StrategyType -
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()Acquire an entry from the pool.Utility method to acquire an entry from the pool, reserving and creating a new entry if necessary.acquireAt(int idx) Deprecated.No longer supported.voidclose()voiddump(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.intintintintintintGet the maximum number of times the entries of the pool can be acquired.intbooleanisClosed()booleanThis method will return an acquired object to the pool.booleanRemove a value from the pool.reserve()Create a new disabled slot into the pool.reserve(int allotment) Deprecated.Usereserve()insteadfinal voidsetMaxMultiplex(int maxMultiplex) final voidsetMaxUsageCount(int maxUsageCount) Change the max usage count of the pool's entries.intsize()private intstartIndex(int size) toString()Collection<Pool<T>.Entry> values()
-
Field Details
-
LOGGER
-
entries
-
maxEntries
private final int maxEntries -
strategyType
-
locker
-
cache
-
nextIndex
-
closed
private volatile boolean closed -
maxMultiplex
private volatile int maxMultiplex -
maxUsageCount
private volatile int maxUsageCount
-
-
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
public int getReservedCount() -
getIdleCount
public int getIdleCount() -
getInUseCount
public int getInUseCount() -
getClosedCount
public int getClosedCount() -
getMaxEntries
public int getMaxEntries() -
getMaxMultiplex
public int getMaxMultiplex() -
setMaxMultiplex
public final void setMaxMultiplex(int maxMultiplex) -
getMaxUsageCount
public int getMaxUsageCount()Get the maximum number of times the entries of the pool can be acquired.- Returns:
- the max usage count.
-
setMaxUsageCount
public final void setMaxUsageCount(int maxUsageCount) Change the max usage count of the pool's entries. All existing idle entries over this new max usage are removed and closed.- Parameters:
maxUsageCount- the max usage count.
-
reserve
Deprecated.Usereserve()insteadCreate a new disabled slot into the pool. The returned entry must ultimately have thePool.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
Create a new disabled slot into the pool. The returned entry must ultimately have thePool.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
-
acquireAt
Deprecated.No longer supported. Instead use aPool.StrategyTypeto configure the pool.Acquire the entry from the pool at the specified index. This method bypasses the thread-local mechanism.- Parameters:
idx- the index of the entry to acquire.- Returns:
- the specified entry or null if there is none at the specified index or if it is not available.
-
acquire
-
startIndex
private int startIndex(int size) -
acquire
Utility method to acquire 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
This method will return an acquired object to the pool. Objects 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. - Throws:
NullPointerException- if value is null
-
remove
-
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
-