- All Known Implementing Classes:
AbstractRefCounted
,SimpleRefCounted
public interface RefCounted
An interface for objects that need to be notified when all reference
to itself are not in user anymore. This implements basic reference counting
for instance if async operations holding on to services that are close concurrently
but should be functional until all async operations have joined
Classes implementing this interface should ref counted at any time ie. if an object is used it's reference count should
be increased before using it by calling #incRef and a corresponding #decRef must be called in a try/finally
block to release the object again ie.:
inst.incRef(); try { // use the inst... } finally { inst.decRef(); }
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final RefCounted
A noop implementation that always behaves as if it is referenced and cannot be released. -
Method Summary
Modifier and TypeMethodDescriptionboolean
decRef()
Decreases the refCount of this instance.boolean
Returnstrue
only if there was at least one active reference when the method was called; if it returnsfalse
then the object is closed; future attempts to acquire references will fail.void
incRef()
Increments the refCount of this instance.default void
Similar toincRef()
except that it also asserts that it managed to acquire the ref, for use in situations where it is a bug if all refs have been released.boolean
Tries to increment the refCount of this instance.
-
Field Details
-
ALWAYS_REFERENCED
A noop implementation that always behaves as if it is referenced and cannot be released.
-
-
Method Details
-
incRef
void incRef()Increments the refCount of this instance.- Throws:
IllegalStateException
- iff the reference counter can not be incremented.- See Also:
-
tryIncRef
boolean tryIncRef()Tries to increment the refCount of this instance. This method will returntrue
iff the refCount was successfully incremented.- See Also:
-
decRef
boolean decRef()Decreases the refCount of this instance. If the refCount drops to 0, then this instance is considered as closed and should not be used anymore.- Returns:
- returns
true
if the ref count dropped to 0 as a result of calling this method - See Also:
-
hasReferences
boolean hasReferences()Returnstrue
only if there was at least one active reference when the method was called; if it returnsfalse
then the object is closed; future attempts to acquire references will fail.- Returns:
- whether there are currently any active references to this object.
-
mustIncRef
default void mustIncRef()Similar toincRef()
except that it also asserts that it managed to acquire the ref, for use in situations where it is a bug if all refs have been released.
-