Class ConcurrentPool<P>

java.lang.Object
org.eclipse.jetty.util.ConcurrentPool<P>
Type Parameters:
P - the type of the pooled objects
All Implemented Interfaces:
Dumpable, Pool<P>

@ManagedObject public class ConcurrentPool<P> extends Object implements Pool<P>, Dumpable

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

  • Field Details

    • OPTIMAL_MAX_SIZE

      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.
      See Also:
  • Constructor Details

    • ConcurrentPool

      public ConcurrentPool(ConcurrentPool.StrategyType strategyType, int maxSize)

      Creates an instance with the specified strategy.

      Parameters:
      strategyType - the strategy to used to lookup entries
      maxSize - the maximum number of pooled entries
    • ConcurrentPool

      @Deprecated public ConcurrentPool(ConcurrentPool.StrategyType strategyType, int maxSize, boolean cache)
      Deprecated.
      cache is no longer supported. Use ConcurrentPool.StrategyType.THREAD_ID

      Creates an instance with the specified strategy.

      Parameters:
      strategyType - the strategy to used to lookup entries
      maxSize - the maximum number of pooled entries
      cache - whether a ThreadLocal cache should be used for the most recently released entry
    • ConcurrentPool

      @Deprecated public ConcurrentPool(ConcurrentPool.StrategyType strategyType, int maxSize, boolean cache, ToIntFunction<P> maxMultiplex)
      Deprecated.
      cache is no longer supported. Use ConcurrentPool.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 - the strategy to used to lookup entries
      maxSize - the maximum number of pooled entries
      cache - whether a ThreadLocal cache should be used for the most recently released entry
      maxMultiplex - a function that given the pooled object returns the max multiplex count
    • ConcurrentPool

      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 - the strategy to used to lookup entries
      maxSize - the maximum number of pooled entries
      maxMultiplex - a function that given the pooled object returns the max multiplex count
  • Method Details

    • getLeaked

      @ManagedAttribute("number of entries leaked (not released nor referenced)") public long getLeaked()
    • leaked

      protected void leaked()
    • reserve

      public Pool.Entry<P> reserve()
      Description copied from interface: Pool

      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 via Pool.Entry.remove().

      Specified by:
      reserve in interface Pool<P>
      Returns:
      a disabled entry that is contained in the pool, or null if the pool is terminated or if the pool cannot reserve an entry
    • acquire

      public Pool.Entry<P> acquire()
      Description copied from interface: Pool

      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.

      Specified by:
      acquire in interface Pool<P>
      Returns:
      an entry from the pool or null if none is available.
    • isTerminated

      public boolean isTerminated()
      Specified by:
      isTerminated in interface Pool<P>
      Returns:
      whether this Pool has been terminated
      See Also:
    • terminate

      public Collection<Pool.Entry<P>> terminate()
      Description copied from interface: Pool

      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.

      Specified by:
      terminate in interface Pool<P>
      Returns:
      a list of all entries
    • size

      public int size()
      Specified by:
      size in interface Pool<P>
      Returns:
      the current number of entries in this Pool
    • getMaxSize

      public int getMaxSize()
      Specified by:
      getMaxSize in interface Pool<P>
      Returns:
      the maximum number of entries in this Pool
    • stream

      public Stream<Pool.Entry<P>> stream()
      Specified by:
      stream in interface Pool<P>
      Returns:
      a Stream over the entries
    • getReservedCount

      public int getReservedCount()
      Specified by:
      getReservedCount in interface Pool<P>
      Returns:
      the number of reserved entries
    • getIdleCount

      public int getIdleCount()
      Specified by:
      getIdleCount in interface Pool<P>
      Returns:
      the number of idle entries
    • getInUseCount

      public int getInUseCount()
      Specified by:
      getInUseCount in interface Pool<P>
      Returns:
      the number of in-use entries
    • getTerminatedCount

      public int getTerminatedCount()
      Specified by:
      getTerminatedCount in interface Pool<P>
      Returns:
      the number of terminated entries
    • dump

      public void dump(Appendable out, String indent) throws IOException
      Description copied from interface: Dumpable
      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.
      Specified by:
      dump in interface Dumpable
      Parameters:
      out - The appendable to dump to
      indent - The indent to apply after any new lines.
      Throws:
      IOException - if unable to write to Appendable
    • toString

      public String toString()
      Overrides:
      toString in class Object