Class SharedResourceHolder
- java.lang.Object
-
- io.grpc.internal.SharedResourceHolder
-
@ThreadSafe public final class SharedResourceHolder extends java.lang.Object
A holder for shared resource singletons.Components like client channels and servers need certain resources, e.g. a thread pool, to run. If the user has not provided such resources, these components will use a default one, which is shared as a static resource. This class holds these default resources and manages their life-cycles.
A resource is identified by the reference of a
SharedResourceHolder.Resource
object, which is typically a singleton, provided to the get() and release() methods. Each Resource object (not its class) maps to an object cached in the holder.Resources are ref-counted and shut down after a delay when the ref-count reaches zero.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SharedResourceHolder.Instance
static interface
SharedResourceHolder.Resource<T>
Defines a resource, and the way to create and destroy instances of it.(package private) static interface
SharedResourceHolder.ScheduledExecutorFactory
-
Field Summary
Fields Modifier and Type Field Description (package private) static long
DESTROY_DELAY_SECONDS
private java.util.concurrent.ScheduledExecutorService
destroyer
private SharedResourceHolder.ScheduledExecutorFactory
destroyerFactory
private static SharedResourceHolder
holder
private java.util.IdentityHashMap<SharedResourceHolder.Resource<?>,SharedResourceHolder.Instance>
instances
-
Constructor Summary
Constructors Constructor Description SharedResourceHolder(SharedResourceHolder.ScheduledExecutorFactory destroyerFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> T
get(SharedResourceHolder.Resource<T> resource)
Try to get an existing instance of the given resource.(package private) <T> T
getInternal(SharedResourceHolder.Resource<T> resource)
Visible to unit tests.static <T> T
release(SharedResourceHolder.Resource<T> resource, T instance)
Releases an instance of the given resource.(package private) <T> T
releaseInternal(SharedResourceHolder.Resource<T> resource, T instance)
Visible to unit tests.
-
-
-
Field Detail
-
DESTROY_DELAY_SECONDS
static final long DESTROY_DELAY_SECONDS
- See Also:
- Constant Field Values
-
holder
private static final SharedResourceHolder holder
-
instances
private final java.util.IdentityHashMap<SharedResourceHolder.Resource<?>,SharedResourceHolder.Instance> instances
-
destroyerFactory
private final SharedResourceHolder.ScheduledExecutorFactory destroyerFactory
-
destroyer
private java.util.concurrent.ScheduledExecutorService destroyer
-
-
Constructor Detail
-
SharedResourceHolder
SharedResourceHolder(SharedResourceHolder.ScheduledExecutorFactory destroyerFactory)
-
-
Method Detail
-
get
public static <T> T get(SharedResourceHolder.Resource<T> resource)
Try to get an existing instance of the given resource. If an instance does not exist, create a new one with the given factory.- Parameters:
resource
- the singleton object that identifies the requested static resource
-
release
public static <T> T release(SharedResourceHolder.Resource<T> resource, T instance)
Releases an instance of the given resource.The instance must have been obtained from
get(Resource)
. Otherwise will throw IllegalArgumentException.Caller must not release a reference more than once. It's advisory that you clear the reference to the instance with the null returned by this method.
- Parameters:
resource
- the singleton Resource object that identifies the released static resourceinstance
- the released static resource- Returns:
- a null which the caller can use to clear the reference to that instance.
-
getInternal
<T> T getInternal(SharedResourceHolder.Resource<T> resource)
Visible to unit tests.- See Also:
get(Resource)
-
releaseInternal
<T> T releaseInternal(SharedResourceHolder.Resource<T> resource, T instance)
Visible to unit tests.
-
-