Top Description Inners Fields Constructors Methods
org.eclipse.jetty.util

public Class ConcurrentPool<P>

extends Object
implements Pool<P>, Dumpable
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.Pool
Annotations
@ManagedObject
Type Parameters
<P>
the type of the pooled objects
Imports
java.io.IOException, java.lang.ref.WeakReference, java.util.Collection, .List, .Objects, java.util.concurrent.CopyOnWriteArrayList, .ThreadLocalRandom, java.util.concurrent.atomic.AtomicInteger, .LongAdder, java.util.function.ToIntFunction, java.util.stream.Stream, org.eclipse.jetty.util.annotation.ManagedAttribute, .ManagedObject, org.eclipse.jetty.util.component.Dumpable, .DumpableCollection, org.eclipse.jetty.util.thread.AutoLock, org.slf4j.Logger, .LoggerFactory

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().

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ConcurrentPool.ConcurrentEntry<E>

A Pool entry that holds metadata and a pooled object.

private static class
ConcurrentPool.Holder<P>

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.

public static enum
ConcurrentPool.StrategyType

The type of the strategy to use for the pool.

Field Summary

Modifier and TypeField and Description
private final List<ConcurrentPool.Holder<P>>
private final LongAdder
private final AutoLock
private static final Logger
private final ToIntFunction<P>
private final int
private final AtomicInteger
public static final int
OPTIMAL_MAX_SIZE

ConcurrentPool internally needs to linearly scan a list to perform an acquisition.

private final ConcurrentPool.StrategyType
private volatile boolean

Constructor Summary

AccessConstructor and Description
public
ConcurrentPool(ConcurrentPool.StrategyType
the strategy to used to lookup entries
strategyType
,
int
the maximum number of pooled entries
maxSize
)

Creates an instance with the specified strategy.

public
ConcurrentPool(ConcurrentPool.StrategyType
the strategy to used to lookup entries
strategyType
,
int
the maximum number of pooled entries
maxSize
,
boolean
whether a ThreadLocal cache should be used for the most recently released entry
cache
)

Deprecated cache is no longer supported. Use StrategyType#THREAD_ID

Creates an instance with the specified strategy.

public
ConcurrentPool(ConcurrentPool.StrategyType
the strategy to used to lookup entries
strategyType
,
int
the maximum number of pooled entries
maxSize
,
boolean
whether a ThreadLocal cache should be used for the most recently released entry
cache
,
ToIntFunction<P>
a 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.StrategyType
the strategy to used to lookup entries
strategyType
,
int
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.

Method Summary

Modifier and TypeMethod and Description
public Pool.Entry<P>
acquire()

Implements org.eclipse.jetty.util.Pool.acquire.

Acquires an entry from the pool.

public void
dump(Appendable
The appendable to dump to
out
,
String
The indent to apply after any new lines.
indent
)

Implements org.eclipse.jetty.util.component.Dumpable.dump.

Dump this object (and children) into an Appendable using the provided indent after any new lines.
public long
private int

Returns:

the max multiplex count for the given pooled object
getMaxMultiplex
(P
the pooled object
pooled
)

Retrieves the max multiplex count for the given pooled object.

public int
public boolean
private void
private boolean
release(Pool.Entry<P> entry)

private boolean
remove(Pool.Entry<P> entry)

public Pool.Entry<P>
reserve()

Implements org.eclipse.jetty.util.Pool.reserve.

Creates a new disabled slot into the pool.

public int
private int
startIndex(int size)

public Stream<Pool.Entry<P>>
pack-priv void
public Collection<Pool.Entry<P>>
terminate()

Implements org.eclipse.jetty.util.Pool.terminate.

Terminates this Pool.

private boolean
terminate(Pool.Entry<P> entry)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

entriesback to summary
private final List<ConcurrentPool.Holder<P>> entries
leakedback to summary
private final LongAdder leaked
lockback to summary
private final AutoLock lock
LOGback to summary
private static final Logger LOG
maxMultiplexback to summary
private final ToIntFunction<P> maxMultiplex
maxSizeback to summary
private final int maxSize
nextIndexback to summary
private final AtomicInteger nextIndex
OPTIMAL_MAX_SIZEback to summary
public static final int OPTIMAL_MAX_SIZE

ConcurrentPool internally needs to linearly scan a list to perform an acquisition. This list needs to be reasonably short otherwise there is a risk that scanning the list becomes a bottleneck. Instances created with a size at most this value should be immune to this problem.

strategyTypeback to summary
private final ConcurrentPool.StrategyType strategyType
terminatedback to summary
private volatile boolean terminated

Constructor Detail

ConcurrentPoolback to summary
public ConcurrentPool(ConcurrentPool.StrategyType strategyType, int maxSize)

Creates an instance with the specified strategy.

Parameters
strategyType:ConcurrentPool.StrategyType

the strategy to used to lookup entries

maxSize:int

the maximum number of pooled entries

ConcurrentPoolback to summary
public ConcurrentPool(ConcurrentPool.StrategyType strategyType, int maxSize, boolean cache)

Deprecated

cache is no longer supported. Use StrategyType#THREAD_ID

Creates an instance with the specified strategy.

Parameters
strategyType:ConcurrentPool.StrategyType

the strategy to used to lookup entries

maxSize:int

the maximum number of pooled entries

cache:boolean

whether a ThreadLocal cache should be used for the most recently released entry

Annotations
@Deprecated
ConcurrentPoolback to summary
public ConcurrentPool(ConcurrentPool.StrategyType strategyType, int maxSize, boolean cache, ToIntFunction<P> maxMultiplex)

Deprecated

cache is no longer supported. Use StrategyType#THREAD_ID

Creates an instance with the specified strategy. and a function that returns the max multiplex count for a given pooled object.

Parameters
strategyType:ConcurrentPool.StrategyType

the strategy to used to lookup entries

maxSize:int

the maximum number of pooled entries

cache:boolean

whether a ThreadLocal cache should be used for the most recently released entry

maxMultiplex:ToIntFunction<P>

a function that given the pooled object returns the max multiplex count

Annotations
@Deprecated
ConcurrentPoolback to summary
public ConcurrentPool(ConcurrentPool.StrategyType strategyType, int maxSize, ToIntFunction<P> maxMultiplex)

Creates an instance with the specified strategy. and a function that returns the max multiplex count for a given pooled object.

Parameters
strategyType:ConcurrentPool.StrategyType

the strategy to used to lookup entries

maxSize:int

the maximum number of pooled entries

maxMultiplex:ToIntFunction<P>

a function that given the pooled object returns the max multiplex count

Method Detail

acquireback to summary
public Pool.Entry<P> acquire()

Implements org.eclipse.jetty.util.Pool.acquire.

Doc from org.eclipse.jetty.util.Pool.acquire.

Acquires an entry from the pool.

Only enabled entries will be returned from this method and their Entry#enable(Object, boolean) method must not be called.

Returns:Pool.Entry<P>

an entry from the pool or null if none is available.

Annotations
@Override
dumpback to summary
public void dump(Appendable out, String indent) throws IOException

Implements org.eclipse.jetty.util.component.Dumpable.dump.

Doc from org.eclipse.jetty.util.component.Dumpable.dump.

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.

Parameters
out:Appendable

The appendable to dump to

indent:String

The indent to apply after any new lines.

Annotations
@Override
Exceptions
IOException:
if unable to write to Appendable
getLeakedback to summary
public long getLeaked()
Annotations
@ManagedAttribute:number of entries leaked (not released nor referenced)
getMaxMultiplexback to summary
private int getMaxMultiplex(P pooled)

Retrieves the max multiplex count for the given pooled object.

Parameters
pooled:P

the pooled object

Returns:int

the max multiplex count for the given pooled object

getMaxSizeback to summary
public int getMaxSize()

Implements org.eclipse.jetty.util.Pool.getMaxSize.

Returns:int

Doc from org.eclipse.jetty.util.Pool.getMaxSize.

the maximum number of entries in this Pool

Annotations
@Override
isTerminatedback to summary
public boolean isTerminated()

Implements org.eclipse.jetty.util.Pool.isTerminated.

Returns:boolean

Doc from org.eclipse.jetty.util.Pool.isTerminated.

whether this Pool has been terminated

Annotations
@Override
leakedback to summary
private void leaked(ConcurrentPool.Holder<P> holder)
releaseback to summary
private boolean release(Pool.Entry<P> entry)
removeback to summary
private boolean remove(Pool.Entry<P> entry)
reserveback to summary
public Pool.Entry<P> reserve()

Implements org.eclipse.jetty.util.Pool.reserve.

Doc from org.eclipse.jetty.util.Pool.reserve.

Creates a new disabled slot into the pool.

The returned entry must ultimately have the Entry#enable(Object, boolean) method called or be removed via Pool.Entry#remove().

Returns:Pool.Entry<P>

a disabled entry that is contained in the pool, or null if the pool is terminated or if the pool cannot reserve an entry

Annotations
@Override
sizeback to summary
public int size()

Implements org.eclipse.jetty.util.Pool.size.

Returns:int

Doc from org.eclipse.jetty.util.Pool.size.

the current number of entries in this Pool

Annotations
@Override
startIndexback to summary
private int startIndex(int size)
streamback to summary
public Stream<Pool.Entry<P>> stream()

Implements org.eclipse.jetty.util.Pool.stream.

Returns:Stream<Pool.Entry<P>>

Doc from org.eclipse.jetty.util.Pool.stream.

a Stream over the entries

Annotations
@Override
sweepback to summary
pack-priv void sweep()
terminateback to summary
public Collection<Pool.Entry<P>> terminate()

Implements org.eclipse.jetty.util.Pool.terminate.

Doc from org.eclipse.jetty.util.Pool.terminate.

Terminates this Pool.

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.

Returns:Collection<Pool.Entry<P>>

a list of all entries

Annotations
@Override
terminateback to summary
private boolean terminate(Pool.Entry<P> entry)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.eclipse.jetty.util back to summary

public Class ConcurrentPool.ConcurrentEntry<E>

extends Object
implements Pool.Entry<E>
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.util.Pool.Entry

A Pool entry that holds metadata and a pooled object.

Field Summary

Modifier and TypeField and Description
private final ConcurrentPool.Holder<E>
private final ConcurrentPool<E>
private E
private final AtomicBiInteger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
enable(E
the pooled object for this Entry
pooled
,
boolean
whether this Entry should be atomically enabled and acquired
acquire
)

Implements org.eclipse.jetty.util.Pool.Entry.enable.

Enables this, previously reserved, Entry.

private ConcurrentPool.Holder<E>
public E
public boolean
public boolean
public boolean
public boolean
public boolean
release()

Implements org.eclipse.jetty.util.Pool.Entry.release.

Releases this Entry to the Pool.

public boolean
remove()

Implements org.eclipse.jetty.util.Pool.Entry.remove.

Removes this Entry from the Pool.

private boolean
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
private boolean

Returns:

whether this Entry was acquired
tryAcquire
()

Tries to acquire this Entry.

private boolean

Returns:

whether this Entry was enabled
tryEnable
(boolean
whether to also acquire this Entry
acquire
)

Tries to enable, and possible also acquire, this Entry.

private boolean

Returns:

true if this Entry was released, false if tryRemove() should be called.
tryRelease
()

Tries to release this Entry.

private boolean

Returns:

whether this Entry can be removed
tryRemove
()

Tries to remove this Entry by marking it as terminated.

private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

holderback to summary
private final ConcurrentPool.Holder<E> holder
poolback to summary
private final ConcurrentPool<E> pool
pooledback to summary
private E pooled
stateback to summary
private final AtomicBiInteger state

Constructor Detail

ConcurrentEntryback to summary
public ConcurrentEntry(ConcurrentPool<E> pool)

Method Detail

enableback to summary
public boolean enable(E pooled, boolean acquire)

Implements org.eclipse.jetty.util.Pool.Entry.enable.

Doc from org.eclipse.jetty.util.Pool.Entry.enable.

Enables this, previously reserved, Entry.

An entry returned from the reserve() method must be enabled with this method, once and only once, before it is usable by the pool.

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.

Parameters
pooled:E

the pooled object for this Entry

acquire:boolean

whether this Entry should be atomically enabled and acquired

Returns:boolean

whether this Entry was enabled

Annotations
@Override
getHolderback to summary
private ConcurrentPool.Holder<E> getHolder()
getPooledback to summary
public E getPooled()

Implements org.eclipse.jetty.util.Pool.Entry.getPooled.

Returns:E

Doc from org.eclipse.jetty.util.Pool.Entry.getPooled.

the pooled object

Annotations
@Override
isIdleback to summary
public boolean isIdle()

Implements org.eclipse.jetty.util.Pool.Entry.isIdle.

Returns:boolean

Doc from org.eclipse.jetty.util.Pool.Entry.isIdle.

whether this Entry is idle in the Pool

Annotations
@Override
isInUseback to summary
public boolean isInUse()

Implements org.eclipse.jetty.util.Pool.Entry.isInUse.

Returns:boolean

Doc from org.eclipse.jetty.util.Pool.Entry.isInUse.

whether this Entry is in use

Annotations
@Override
isReservedback to summary
public boolean isReserved()

Implements org.eclipse.jetty.util.Pool.Entry.isReserved.

Returns:boolean

Doc from org.eclipse.jetty.util.Pool.Entry.isReserved.

whether this Entry is reserved

Annotations
@Override
isTerminatedback to summary
public boolean isTerminated()

Implements org.eclipse.jetty.util.Pool.Entry.isTerminated.

Returns:boolean

Doc from org.eclipse.jetty.util.Pool.Entry.isTerminated.

whether this Entry is terminated

Annotations
@Override
releaseback to summary
public boolean release()

Implements org.eclipse.jetty.util.Pool.Entry.release.

Doc from org.eclipse.jetty.util.Pool.Entry.release.

Releases this Entry to the Pool.

Returns:boolean

whether this Entry was released

Annotations
@Override
removeback to summary
public boolean remove()

Implements org.eclipse.jetty.util.Pool.Entry.remove.

Doc from org.eclipse.jetty.util.Pool.Entry.remove.

Removes this Entry from the Pool.

Returns:boolean

whether this Entry was removed

Annotations
@Override
terminateback to summary
private boolean terminate()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
tryAcquireback to summary
private boolean tryAcquire()

Tries to acquire this Entry.

Returns:boolean

whether this Entry was acquired

tryEnableback to summary
private boolean tryEnable(boolean acquire)

Tries to enable, and possible also acquire, this Entry.

Parameters
acquire:boolean

whether to also acquire this Entry

Returns:boolean

whether this Entry was enabled

tryReleaseback to summary
private boolean tryRelease()

Tries to release this Entry.

Returns:boolean

true if this Entry was released, false if tryRemove() should be called.

tryRemoveback to summary
private boolean tryRemove()

Tries to remove this Entry by marking it as terminated.

Returns:boolean

whether this Entry can be removed

tryTerminateback to summary
private boolean tryTerminate()
org.eclipse.jetty.util back to summary

private Class ConcurrentPool.Holder<P>

extends Object
Class Inheritance

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.

Field Summary

Modifier and TypeField and Description
private volatile ConcurrentPool.ConcurrentEntry<P>
private final WeakReference<ConcurrentPool.ConcurrentEntry<P>>

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public void
acquired()

Called when an entry is acquired from the pool with multiplex count going from 0 to 1.

public Pool.Entry<P>
public void
released()

Called when an entry is released to the pool with multiplex count going from 1 to 0.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

_strongback to summary
private volatile ConcurrentPool.ConcurrentEntry<P> _strong
_weakback to summary
private final WeakReference<ConcurrentPool.ConcurrentEntry<P>> _weak

Constructor Detail

Holderback to summary
protected Holder(ConcurrentPool.ConcurrentEntry<P> entry)

Method Detail

acquiredback to summary
public void acquired()

Called when an entry is acquired from the pool with multiplex count going from 0 to 1.

getEntryback to summary
public Pool.Entry<P> getEntry()
releasedback to summary
public void released()

Called when an entry is released to the pool with multiplex count going from 1 to 0.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.eclipse.jetty.util back to summary

public final Enum ConcurrentPool.StrategyType

extends Enum<ConcurrentPool.StrategyType>
Class Inheritance

The type of the strategy to use for the pool. The strategy primarily determines where iteration over the pool entries begins.

Field Summary

Modifier and TypeField and Description
public static final ConcurrentPool.StrategyType
FIRST

A strategy that looks for an entry always starting from the first entry.

public static final ConcurrentPool.StrategyType
RANDOM

A strategy that looks for an entry by iterating from a random starting index.

public static final ConcurrentPool.StrategyType
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.StrategyType
THREAD_ID

A strategy that uses the Thread#getId() of the current thread to select a starting point for an entry search.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ConcurrentPool.StrategyType
public static ConcurrentPool.StrategyType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

FIRSTback to summary
public static final ConcurrentPool.StrategyType FIRST

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.

RANDOMback to summary
public static final ConcurrentPool.StrategyType RANDOM

A strategy that looks for an entry by iterating from a random starting index. No entries are favoured and contention is reduced.

ROUND_ROBINback to summary
public static final ConcurrentPool.StrategyType ROUND_ROBIN

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_IDback to summary
public static final ConcurrentPool.StrategyType THREAD_ID

A strategy that uses the Thread#getId() of the current thread to select a starting point for an entry search. No entries are favoured and contention is reduced.

Constructor Detail

StrategyTypeback to summary
private StrategyType()

Method Detail

valueOfback to summary
public static ConcurrentPool.StrategyType valueOf(String name)
valuesback to summary
public static ConcurrentPool.StrategyType[] values()