Class ReferenceCounted
java.lang.Object
io.deephaven.util.referencecounting.ReferenceCounted
- All Implemented Interfaces:
LogOutputAppendable
- Direct Known Subclasses:
BaseChunkInputStreamGenerator
,JobScheduler.IterationManager
,ProceduralReferenceCounted
,ReferenceCountedLivenessReferent
Implements a recurring reference counting pattern - a concurrent reference count that should refuse to go below zero,
and invokes
onReferenceCountAtZero()
exactly once when the count returns to zero.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
Decrement the reference count by one, if it has ever been increased and has not already been decreased to zero.final boolean
Force the reference count to zero.static String
getReferenceCountDebug
(Object maybeReferenceCounted) final void
Increment the reference count by one, if it has not already been decreased to zero.protected abstract void
Callback method that will be invoked when the reference count returns to zero.final void
Reset this reference count to its initial state for reuse.toString()
final boolean
Decrement the reference count by one, if it has ever been increased and has not already been decreased to zero.final boolean
Increment the reference count by 1, if it has not already been decreased to 0.
-
Constructor Details
-
ReferenceCounted
protected ReferenceCounted() -
ReferenceCounted
protected ReferenceCounted(int initialValue) - Parameters:
initialValue
- The initial value for the reference count, taken as an unsigned integer. Must not be one of the reserved values -1 or -2.
-
-
Method Details
-
toString
-
append
- Specified by:
append
in interfaceLogOutputAppendable
-
getReferenceCountDebug
-
resetReferenceCount
public final void resetReferenceCount()Reset this reference count to its initial state for reuse. -
tryIncrementReferenceCount
public final boolean tryIncrementReferenceCount()Increment the reference count by 1, if it has not already been decreased to 0.- Returns:
- Whether the reference count was successfully incremented
- Throws:
IllegalStateException
- If the reference count is already at its maximum referenceCount
-
incrementReferenceCount
public final void incrementReferenceCount()Increment the reference count by one, if it has not already been decreased to zero.- Throws:
IllegalStateException
- If the reference count was not successfully incremented
-
tryDecrementReferenceCount
public final boolean tryDecrementReferenceCount()Decrement the reference count by one, if it has ever been increased and has not already been decreased to zero. Invokes the implementation'sonReferenceCountAtZero()
method if decrementing to zero.- Returns:
- Whether the reference count was successfully decremented
-
forceReferenceCountToZero
public final boolean forceReferenceCountToZero()Force the reference count to zero. If it was non-zero, this will have the same side effects as returning to zero normally, but subsequent invocations ofdecrementReferenceCount()
andtryDecrementReferenceCount()
will act as if the reference count was successfully decremented untilresetReferenceCount()
is invoked.- Returns:
- Whether this invocation actually forced the reference count to zero (and invoked
onReferenceCountAtZero()
.false
means that this ReferenceCounted reached a zero through other means.
-
decrementReferenceCount
public final void decrementReferenceCount()Decrement the reference count by one, if it has ever been increased and has not already been decreased to zero. Invokes the implementation'sonReferenceCountAtZero()
method if decrementing to zero.- Throws:
IllegalStateException
- If the reference count was not successfully decremented
-
onReferenceCountAtZero
protected abstract void onReferenceCountAtZero()Callback method that will be invoked when the reference count returns to zero.
-