Class ThreadIdPool<E>

java.lang.Object
org.eclipse.jetty.util.thread.ThreadIdPool<E>
All Implemented Interfaces:
Dumpable

public class ThreadIdPool<E> extends Object implements Dumpable
A fixed sized pool of items that uses ThreadId to avoid contention. This class can be used, instead of a ThreadLocal, when pooling items that are expensive to create, but only used briefly in the scope of a single thread. It is safe to use with VirtualThreads, as unlike a ThreadLocal pool, the number of items is limited.

This is a light-weight version of ConcurrentPool that is best used when items do not reserve an index in the pool even when acquired.

See Also:
  • Constructor Details

    • ThreadIdPool

      public ThreadIdPool()
    • ThreadIdPool

      public ThreadIdPool(int capacity)
  • Method Details

    • capacity

      public int capacity()
      Returns:
      the maximum number of items
    • size

      public int size()
      Returns:
      the number of items available
    • offer

      public int offer(E e)
      Offer an item to the pool.
      Parameters:
      e - The item to offer
      Returns:
      The index the item was added at or -1, if it was not added
      See Also:
    • take

      public E take()
      Take an item from the pool.
      Returns:
      The taken item or null if none available.
    • remove

      public boolean remove(E e, int index)
      Remove a specific item from the pool from a specific index
      Parameters:
      e - The item to remove
      index - The index the item was given to, as returned by offer(Object)
      Returns:
      True if the item was in the pool and was able to be removed.
    • removeAll

      public List<E> removeAll()
      Removes all items from the pool.
      Returns:
      A list of all removed items
    • takeOrElse

      public E takeOrElse(Supplier<E> supplier)
      Take an item with a take() operation, else if that returns null then use the supplier (which may construct a new instance).
      Parameters:
      supplier - The supplier for an item to be used if an item cannot be taken from the pool.
      Returns:
      An item, never null.
    • apply

      public <R> R apply(Supplier<E> supplier, Function<E,R> function)
      Apply an item, either from the pool or supplier, to a function, then give it back to the pool. This is equivalent of takeOrElse(Supplier); then Function.apply(Object); followed by offer(Object).
      Type Parameters:
      R - The type of the function return
      Parameters:
      supplier - The supplier for an item to be used if an item cannot be taken from the pool.
      function - A function producing a result from an item. This may be a method reference to a method on the item taking no arguments and producing a result.
      Returns:
      Te result of the function applied to the item and the argument
    • apply

      public <A, R> R apply(Supplier<E> supplier, BiFunction<E,A,R> function, A argument)
      Apply an item, either from the pool or supplier, to a function, then give it back to the pool. This is equivalent of takeOrElse(Supplier); then BiFunction.apply(Object, Object); followed by offer(Object).
      Type Parameters:
      A - The type of the function argument
      R - The type of the function return
      Parameters:
      supplier - The supplier for an item to be used if an item cannot be taken from the pool.
      function - A function producing a result from an item and an argument. This may be a method reference to a method on the item taking an argument and producing a result.
      argument - The argument to pass to the function.
      Returns:
      Te result of the function applied to the item and the argument
    • 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