A concurrent implementation of Pool.
This implementation offers a number of strategies
used to select the entry returned from acquire(), and its
capacity is bounded to a max size.
When a pooled item is acquired from this pool, it is only held
by a WeakReference, so that if it is collected before being released,
then that leak is detected and the entry is removed (see getLeaked().
| Modifier and Type | Class and Description |
|---|---|
| public static class | |
| private static class | ConcurrentPool.Holder<P>
Holds a strong and a weak reference to an |
| public static enum | ConcurrentPool.StrategyType
The type of the strategy to use for the pool. |
| Modifier and Type | Field and Description |
|---|---|
| private final List | |
| private final LongAdder | |
| private final AutoLock | |
| private static final Logger | |
| private final ToIntFunction | |
| private final int | |
| private final AtomicInteger | |
| public static final int | OPTIMAL_MAX_SIZE
|
| private final ConcurrentPool. | |
| private volatile boolean |
| Access | Constructor and Description |
|---|---|
| public | ConcurrentPool(ConcurrentPool.
the strategy to used to lookup entries strategyType,the maximum number of pooled entries maxSize)Creates an instance with the specified strategy. |
| public | ConcurrentPool(ConcurrentPool.
the strategy to used to lookup entries strategyType,the maximum number of pooled entries maxSize, boolean whether a cache)ThreadLocal cache should be used for the most recently released entry
Deprecated
cache is no longer supported. Use
StrategyType#THREAD_ID
Creates an instance with the specified strategy. |
| public | ConcurrentPool(ConcurrentPool.
the strategy to used to lookup entries strategyType,the maximum number of pooled entries maxSize, boolean whether a cache, ToIntFunction<P> ThreadLocal cache should be used for the most recently released entrya function that given the pooled object returns the max multiplex count maxMultiplex)
Deprecated
cache is no longer supported. Use
StrategyType#THREAD_ID
Creates an instance with the specified strategy. |
| public | ConcurrentPool(ConcurrentPool.
the strategy to used to lookup entries strategyType,the maximum number of pooled entries maxSize, ToIntFunction<P> a function that given the pooled object returns the max multiplex count maxMultiplex)Creates an instance with the specified strategy. |
| Modifier and Type | Method and Description |
|---|---|
| public Pool. | |
| public void | dump(Appendable
The appendable to dump to out, String The indent to apply after any new lines. indent)Implements org. |
| public long | |
| private int | Returns: the max multiplex count for the given pooled objectthe pooled object pooled)Retrieves the max multiplex count for the given pooled object. |
| public int | |
| public boolean | |
| private void | |
| private boolean | |
| private boolean | |
| public Pool. | |
| public int | |
| private int | |
| public Stream | |
| pack-priv void | |
| public Collection | |
| private boolean | |
| public String |
| entries | back to summary |
|---|---|
| private final List<ConcurrentPool. | |
| leaked | back to summary |
|---|---|
| private final LongAdder leaked | |
| lock | back to summary |
|---|---|
| private final AutoLock lock | |
| LOG | back to summary |
|---|---|
| private static final Logger LOG | |
| maxMultiplex | back to summary |
|---|---|
| private final ToIntFunction<P> maxMultiplex | |
| maxSize | back to summary |
|---|---|
| private final int maxSize | |
| nextIndex | back to summary |
|---|---|
| private final AtomicInteger nextIndex | |
| OPTIMAL_MAX_SIZE | back to summary |
|---|---|
| public static final int OPTIMAL_MAX_SIZE
| |
| strategyType | back to summary |
|---|---|
| private final ConcurrentPool. | |
| terminated | back to summary |
|---|---|
| private volatile boolean terminated | |
| ConcurrentPool | back to summary |
|---|---|
| public ConcurrentPool(ConcurrentPool. Creates an instance with the specified strategy.
| |
| ConcurrentPool | back to summary |
|---|---|
| public ConcurrentPool(ConcurrentPool. Deprecated cache is no longer supported. Use Creates an instance with the specified strategy.
| |
| ConcurrentPool | back to summary |
|---|---|
| public ConcurrentPool(ConcurrentPool. Deprecated cache is no longer supported. Use Creates an instance with the specified strategy. and a function that returns the max multiplex count for a given pooled object.
| |
| ConcurrentPool | back to summary |
|---|---|
| public ConcurrentPool(ConcurrentPool. Creates an instance with the specified strategy. and a function that returns the max multiplex count for a given pooled object.
| |
| acquire | back to summary |
|---|---|
| public Pool. Implements org. Doc from org. Acquires an entry from the pool. Only enabled entries will be returned from this method
and their | |
| dump | back to summary |
|---|---|
| public void dump(Appendable out, String indent) throws IOException Implements org. Doc from org. Dump 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.
| |
| getLeaked | back to summary |
|---|---|
public long getLeaked()
| |
| getMaxMultiplex | back to summary |
|---|---|
| private int getMaxMultiplex(P pooled) Retrieves the max multiplex count for the given pooled object.
| |
| getMaxSize | back to summary |
|---|---|
| public int getMaxSize() Implements org.
| |
| isTerminated | back to summary |
|---|---|
| public boolean isTerminated() Implements org.
| |
| leaked | back to summary |
|---|---|
| private void leaked(ConcurrentPool. | |
| release | back to summary |
|---|---|
| private boolean release(Pool. | |
| remove | back to summary |
|---|---|
| private boolean remove(Pool. | |
| reserve | back to summary |
|---|---|
| public Pool. Implements org. Doc from org. Creates a new disabled slot into the pool. The returned entry must ultimately have the | |
| size | back to summary |
|---|---|
| public int size() Implements org.
| |
| startIndex | back to summary |
|---|---|
| private int startIndex(int size) | |
| stream | back to summary |
|---|---|
| public Stream Implements org. | |
| sweep | back to summary |
|---|---|
| pack-priv void sweep() | |
| terminate | back to summary |
|---|---|
| public Collection Implements org. Doc from org. Terminates this All the entries are marked as terminated and cannot be acquired nor released, but only removed. The returned list of all entries may be iterated to perform additional operations on the pooled objects. The pool cannot be used anymore after it is terminated.
| |
| terminate | back to summary |
|---|---|
| private boolean terminate(Pool. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
A Pool entry that holds metadata and a pooled object.
| Modifier and Type | Field and Description |
|---|---|
| private final ConcurrentPool. | |
| private final ConcurrentPool | |
| private E | |
| private final AtomicBiInteger |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | enable(E
the pooled object for this pooled, boolean Entrywhether this acquire)Entry should be atomically enabled and acquiredImplements org. Enables this, previously |
| private ConcurrentPool. | |
| public E | |
| public boolean | |
| public boolean | |
| public boolean | |
| public boolean | |
| public boolean | |
| public boolean | |
| private boolean | |
| public String | |
| private boolean | |
| private boolean | Returns: whether thisEntry was enabledwhether to also acquire this acquire)EntryTries to enable, and possible also acquire, this |
| private boolean | |
| private boolean | Returns: whether thisEntry can be removedTries to remove this |
| private boolean |
| holder | back to summary |
|---|---|
| private final ConcurrentPool. | |
| pool | back to summary |
|---|---|
| private final ConcurrentPool<E> pool | |
| pooled | back to summary |
|---|---|
| private E pooled | |
| state | back to summary |
|---|---|
| private final AtomicBiInteger state | |
| ConcurrentEntry | back to summary |
|---|---|
| public ConcurrentEntry(ConcurrentPool<E> pool) | |
| enable | back to summary |
|---|---|
| public boolean enable(E pooled, boolean acquire) Implements org. Doc from org. Enables this, previously An entry returned from the The entry may be enabled and not acquired, in which case it is immediately available to be acquired, potentially by another thread; or it can be enabled and acquired atomically so that no other thread can acquire it, although the acquire may still fail if the pool has been terminated.
| |
| getHolder | back to summary |
|---|---|
| private ConcurrentPool. | |
| getPooled | back to summary |
|---|---|
| public E getPooled() Implements org.
| |
| isIdle | back to summary |
|---|---|
| public boolean isIdle() Implements org.
| |
| isInUse | back to summary |
|---|---|
| public boolean isInUse() Implements org.
| |
| isReserved | back to summary |
|---|---|
| public boolean isReserved() Implements org.
| |
| isTerminated | back to summary |
|---|---|
| public boolean isTerminated() Implements org.
| |
| release | back to summary |
|---|---|
| public boolean release() Implements org. Doc from org. Releases this
| |
| remove | back to summary |
|---|---|
| public boolean remove() Implements org. Doc from org. Removes this
| |
| terminate | back to summary |
|---|---|
| private boolean terminate() | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| tryAcquire | back to summary |
|---|---|
| private boolean tryAcquire() Tries to acquire this
| |
| tryEnable | back to summary |
|---|---|
| private boolean tryEnable(boolean acquire) Tries to enable, and possible also acquire, this
| |
| tryRelease | back to summary |
|---|---|
| private boolean tryRelease() Tries to release this
| |
| tryRemove | back to summary |
|---|---|
| private boolean tryRemove() Tries to remove this
| |
| tryTerminate | back to summary |
|---|---|
| private boolean tryTerminate() | |
Holds a strong and a weak reference to an Entry to avoid holding
on to entries that are not released, so that they can be garbage collected.
Methods released() and acquired() work together to clear the
strong reference when the entry is acquired, and assign it when the entry
is released.
This class handles a race condition happening when an entry is being
released with multiplex count going 1 -> 0 by one thread that
has not yet called released(), and immediately acquired by another
thread that is calling acquired().
The call to acquired() spin loops until released() returns.
| Modifier and Type | Field and Description |
|---|---|
| private volatile ConcurrentPool. | |
| private final WeakReference |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public Pool. | |
| public void | |
| public String |
| _strong | back to summary |
|---|---|
| private volatile ConcurrentPool. | |
| _weak | back to summary |
|---|---|
| private final WeakReference<ConcurrentPool. | |
| Holder | back to summary |
|---|---|
| protected Holder(ConcurrentPool. | |
| acquired | back to summary |
|---|---|
| public void acquired() Called when an entry is acquired from the pool with multiplex count going from | |
| getEntry | back to summary |
|---|---|
| public Pool. | |
| released | back to summary |
|---|---|
| public void released() Called when an entry is released to the pool with multiplex count going from | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| public static final ConcurrentPool. | FIRST
A strategy that looks for an entry always starting from the first entry. |
| public static final ConcurrentPool. | RANDOM
A strategy that looks for an entry by iterating from a random starting index. |
| public static final ConcurrentPool. | ROUND_ROBIN
A strategy that looks for an entry by iterating from a starting point that is incremented on every search. |
| public static final ConcurrentPool. | THREAD_ID
A strategy that uses the |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static ConcurrentPool. | |
| public static ConcurrentPool. |
| FIRST | back to summary |
|---|---|
| public static final ConcurrentPool. A strategy that looks for an entry always starting from the first entry. It will favour the early entries in the pool, but may contend on them more. | |
| RANDOM | back to summary |
|---|---|
| public static final ConcurrentPool. A strategy that looks for an entry by iterating from a random starting index. No entries are favoured and contention is reduced. | |
| ROUND_ROBIN | back to summary |
|---|---|
| public static final ConcurrentPool. A strategy that looks for an entry by iterating from a starting point that is incremented on every search. This gives similar results to the random strategy but with more predictable behaviour. No entries are favoured and contention is reduced. | |
| THREAD_ID | back to summary |
|---|---|
| public static final ConcurrentPool. A strategy that uses the | |
| StrategyType | back to summary |
|---|---|
| private StrategyType() | |
| valueOf | back to summary |
|---|---|
| public static ConcurrentPool. | |
| values | back to summary |
|---|---|
| public static ConcurrentPool. | |