public abstract class AbstractCloseableReferenceCounted extends AbstractReferenceCounted implements ManagedCloseable
This abstract class extends AbstractReferenceCounted
and implements ManagedCloseable
,
and is designed to manage a resource that requires reference counting to ensure proper cleanup
once it is no longer in use.
Reference counting allows multiple users to share a single resource and ensures that the resource is only closed when all references are released. Each user of the resource increments the reference count upon acquiring the resource and decrements it upon releasing.
This class integrates reference counting with the ability to close the resource. When the reference count reaches zero, or if an explicit call to close is made, the resource transitions to the closed state and cannot be used any further.
referenceCounted, WARN_COUNT, WARN_NS
INIT, TMP
DISABLE_SINGLE_THREADED_CHECK
Modifier | Constructor and Description |
---|---|
protected |
AbstractCloseableReferenceCounted()
Constructs a new AbstractCloseableReferenceCounted instance and adds the instance
to the CloseableUtils set for tracking.
|
Modifier and Type | Method and Description |
---|---|
protected void |
backgroundPerformRelease()
Closes the resource in the background.
|
void |
close()
Closes the resource, ensuring it transitions to a closed state.
|
boolean |
isClosed()
Indicates whether the resource is in a closed state.
|
void |
release(ReferenceOwner id)
Releases the resource for the given id.
|
void |
releaseLast(ReferenceOwner id)
Releases the resource for the given id and checks the resource is now released.
|
void |
reserve(ReferenceOwner id)
Attempts to reserve the resource for the given unique id.
|
void |
reserveTransfer(ReferenceOwner from,
ReferenceOwner to)
Reserves the resource for the given id.
|
protected void |
setClosed()
Sets the resource as closed.
|
protected void |
setClosing()
Sets the resource as closing in case it is being released in the background.
|
void |
throwExceptionIfClosed()
Throws an exception if the resource is closed.
|
protected void |
throwExceptionIfClosedInSetter()
Throws an exception if the resource is closed while in a setter method.
|
boolean |
tryReserve(ReferenceOwner id)
Tries to reserve the resource for the given id.
|
addReferenceChangeListener, assertReferencesReleased, canReleaseInBackground, clearUsedByThread, createdHere, disableReferenceTracing, enableReferenceTracing, performRelease, refCount, referenceCountedUnmonitored, referenceId, removeReferenceChangeListener, reservedBy, singleThreadedCheckDisabled, singleThreadedCheckReset, threadSafetyCheck, throwExceptionIfNotReleased, throwExceptionIfReleased, toString, unmonitor, warnAndReleaseIfNotReleased
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
createdHere, warnAndCloseIfNotClosed
closeQuietly, closeQuietly
isClosing
onReleased
releaseLast
referenceName, temporary
protected AbstractCloseableReferenceCounted()
public void reserve(ReferenceOwner id) throws ClosedIllegalStateException, ThreadingIllegalStateException
reserve
in interface ReferenceCounted
reserve
in class AbstractReferenceCounted
id
- the unique identifier representing the owner of this reserve.ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe waypublic void reserveTransfer(ReferenceOwner from, ReferenceOwner to) throws ClosedIllegalStateException, ThreadingIllegalStateException
reserveTransfer
in interface ReferenceCounted
reserveTransfer
in class AbstractReferenceCounted
from
- resourceto
- resourceClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If used in a non thread safe waypublic void release(ReferenceOwner id) throws ClosedIllegalStateException
release
in interface ReferenceCounted
release
in class AbstractReferenceCounted
id
- unique id for this releaseClosedIllegalStateException
- If the resource has been released or closed.public void releaseLast(ReferenceOwner id) throws ClosedIllegalStateException
releaseLast
in interface ReferenceCounted
releaseLast
in class AbstractReferenceCounted
id
- unique id for this releaseClosedIllegalStateException
- If the resource has been released or closed.public boolean tryReserve(ReferenceOwner id) throws ClosedIllegalStateException, IllegalArgumentException
tryReserve
in interface ReferenceCounted
tryReserve
in class AbstractReferenceCounted
id
- unique id for this reserveClosedIllegalStateException
- If the resource has been released or closed.IllegalArgumentException
- If the reference owner is not valid.public void close()
close
in interface Closeable
close
in interface AutoCloseable
AbstractCloseable.performClose()
protected void backgroundPerformRelease()
backgroundPerformRelease
in class AbstractReferenceCounted
protected void setClosing()
protected void setClosed()
public void throwExceptionIfClosed() throws ClosedIllegalStateException, ThreadingIllegalStateException
throwExceptionIfClosed
in interface ManagedCloseable
ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
- If the thread safety check fails.protected void throwExceptionIfClosedInSetter() throws ClosedIllegalStateException, ThreadingIllegalStateException
ClosedIllegalStateException
- If the resource has been released or closed.ThreadingIllegalStateException
public boolean isClosed()
isClosed
in interface QueryCloseable
true
if the resource is closed; false
otherwise.Copyright © 2023. All rights reserved.