Package io.deephaven.util
Class SoftRecycler<T>
java.lang.Object
io.deephaven.util.SoftRecycler<T>
This class makes a little "recycle bin" for your objects of type T. When you want an object, call borrowItem(). When
you do so, either a fresh T will be constructed for you, or a reused T will be pulled from the recycle bin. When you
are done with the object and want to recycle it, call returnItem(). This class will keep a maximum of 'capacity'
items in its recycle bin. Additionally, the items are held by SoftReferences, so the garbage collector may reclaim
them if it feels like it. The items are borrowed in LIFO order, which hopefully is somewhat cache-friendly.
Note that the caller has no special obligation to return a borrowed item nor to return borrowed items in any
particular order. If your code has a need to keep a borrowed item forever, there is no problem with that. But if you
want your objects to be reused, you have to return them.
-
Constructor Summary
ConstructorDescriptionSoftRecycler
(int capacity, Supplier<T> constructItem, Consumer<T> sanitizeItem) -
Method Summary
-
Constructor Details
-
SoftRecycler
- Parameters:
capacity
- Capacity of the recyclerconstructItem
- A callback that creates a new itemsanitizeItem
- Optional. A callback that sanitizes the item before reuse. Pass null if no sanitization is needed.
-
-
Method Details
-
borrowItem
-
returnItem
-