T
- The type of resource this SingletonResource
holds.public class SingletonResource<T> extends Object
A SingletonResource
is a common resource pattern where the first thread that
needs to use a resource instantiates it. Subsequent threads should re-use the resource
instantiated by the first thread.
Modifier and Type | Method and Description |
---|---|
void |
cleanup(Consumer<T> cleaner)
Note(Maithem): We need to remove this method, it shouldn't be used.
|
T |
get()
Get the resource, potentially generating it by calling the
generator if necessary. |
static <R> SingletonResource<R> |
withInitial(Supplier<R> generator)
Factory method with similar semantics as a
ThreadLocal . |
public static <R> SingletonResource<R> withInitial(@Nonnull Supplier<R> generator)
ThreadLocal
.R
- The type of the resource to be provided.generator
- A method to be called when a new R
is needed.SingletonResource
.public T get()
generator
if necessary.SingletonResource
.public void cleanup(@Nonnull Consumer<T> cleaner)
cleaner
- A Consumer
which is provided the resource to perform cleanup
actions.Copyright © 2019 CorfuDB. All rights reserved.