Package org.eclipse.jetty.util.thread
Class ThreadIdPool<E>
java.lang.Object
org.eclipse.jetty.util.thread.ThreadIdPool<E>
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<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.<R> R
Apply an item, either from the pool or supplier, to a function, then give it back to the pool.int
capacity()
void
dump
(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.int
Offer an item to the pool.boolean
Remove a specific item from the pool from a specific indexRemoves all items from the pool.int
size()
take()
Take an item from the pool.takeOrElse
(Supplier<E> supplier) Take an item with atake()
operation, else if that returns null then use thesupplier
(which may construct a new instance).toString()
-
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
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
Take an item from the pool.- Returns:
- The taken item or null if none available.
-
remove
Remove a specific item from the pool from a specific index- Parameters:
e
- The item to removeindex
- The index the item was given to, as returned byoffer(Object)
- Returns:
True
if the item was in the pool and was able to be removed.
-
removeAll
Removes all items from the pool.- Returns:
- A list of all removed items
-
takeOrElse
Take an item with atake()
operation, else if that returns null then use thesupplier
(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
Apply an item, either from the pool or supplier, to a function, then give it back to the pool. This is equivalent oftakeOrElse(Supplier)
; thenFunction.apply(Object)
; followed byoffer(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
Apply an item, either from the pool or supplier, to a function, then give it back to the pool. This is equivalent oftakeOrElse(Supplier)
; thenBiFunction.apply(Object, Object)
; followed byoffer(Object)
.- Type Parameters:
A
- The type of the function argumentR
- 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
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 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
-