public abstract class AbstractReferenceCounted extends Object implements ReferenceCountedTracer, ReferenceOwner, SingleThreadedChecked, Monitorable
This class provides the common functionality required for implementing reference counting mechanisms in resources, such as managing the number of references and releasing resources when they are no longer needed.
| Modifier and Type | Field and Description |
|---|---|
protected MonitorReferenceCounted |
referenceCounted |
protected static int |
WARN_COUNT |
protected static long |
WARN_NS |
INIT, TMPDISABLE_SINGLE_THREADED_CHECK| Modifier | Constructor and Description |
|---|---|
protected |
AbstractReferenceCounted()
Constructs an AbstractReferenceCounted with default monitoring.
|
protected |
AbstractReferenceCounted(boolean monitored)
Constructs an AbstractReferenceCounted.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
Adds a
ReferenceChangeListener that will be notified whenever the reference count changes. |
static void |
assertReferencesReleased()
Asserts that all references have been released.
|
protected void |
backgroundPerformRelease()
Performs the release operation in the background.
|
protected boolean |
canReleaseInBackground()
Returns whether the resource can be released in the background.
|
StackTrace |
createdHere()
Returns the stack trace for where this resource was created.
|
static void |
disableReferenceTracing()
Disables reference tracing.
|
static void |
enableReferenceTracing()
Enables reference tracing.
|
protected abstract void |
performRelease()
Releases the resource.
|
int |
refCount()
Retrieves the current reference count.
|
void |
referenceCountedUnmonitored(boolean unmonitored) |
int |
referenceId()
Returns the unique reference ID for this resource.
|
void |
release(ReferenceOwner id)
Decrements the reference count by one.
|
void |
releaseLast(ReferenceOwner id)
Releases the last reference to the resource.
|
void |
removeReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
Removes a
ReferenceChangeListener previously added via ReferenceCounted.addReferenceChangeListener(ReferenceChangeListener). |
void |
reserve(ReferenceOwner id)
Increments the reference count by one, indicating that the resource is now shared among multiple owners.
|
void |
reserveTransfer(ReferenceOwner from,
ReferenceOwner to)
Transfers a reference from one owner to another.
|
void |
singleThreadedCheckDisabled(boolean singleThreadedCheckDisabled)
Sets the flag to disable the single-threaded check.
|
void |
singleThreadedCheckReset()
Resets the thread-safety check state.
|
protected boolean |
threadSafetyCheck(boolean isUsed) |
void |
throwExceptionIfNotReleased()
Throws an exception if the resource has already been released.
|
void |
throwExceptionIfReleased()
Throws an exception if the resource has been released.
|
@NotNull String |
toString()
Returns a string representation of the object including its reference name.
|
boolean |
tryReserve(ReferenceOwner id)
Attempts to reserve the resource without throwing an exception if it is already released.
|
void |
unmonitor()
Stops monitoring the resource.
|
static void |
unmonitor(ReferenceCounted counted)
Marks a reference-counted resource as unmonitored.
|
void |
warnAndReleaseIfNotReleased()
If not released, logs a warning and releases the resource.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitonReleasedreleaseLastreferenceName, temporaryunmonitorprotected static final long WARN_NS
protected static final int WARN_COUNT
protected final transient MonitorReferenceCounted referenceCounted
protected AbstractReferenceCounted()
protected AbstractReferenceCounted(boolean monitored)
monitored - If true, the resource will be monitored for reference counted release.public static void enableReferenceTracing()
public static void disableReferenceTracing()
NOTE: The resources will still be released appropriately, however if detailed tracing won't be recorded
public static void assertReferencesReleased()
public static void unmonitor(ReferenceCounted counted)
NOTE: The resource will still be released appropriately, however it won't give a warning if it is not.
counted - the resource to unmonitor.public int referenceId()
referenceId in interface ReferenceOwnerpublic StackTrace createdHere()
createdHere in interface ReferenceCountedTracerpublic void throwExceptionIfNotReleased()
throws IllegalStateException
throwExceptionIfNotReleased in interface ReferenceCountedTracerClosedIllegalStateException - If the resource has been released or closed.ThreadingIllegalStateException - If this resource was accessed by multiple threads in an unsafe wayIllegalStateException - If the object has not been released (i.e., its reference count is greater than 0).protected void backgroundPerformRelease()
protected boolean canReleaseInBackground()
true if the resource can be released in the background, false otherwise.protected abstract void performRelease()
throws IllegalStateException
IllegalStateException - if the resource cannot be released.public void reserve(ReferenceOwner id) throws ClosedIllegalStateException, ThreadingIllegalStateException
When tracing is enabled, it checks the same owner doesn't try to reserve it twice (without releasing it in the meantime)
reserve in interface ReferenceCountedid - The reference owner.ClosedIllegalStateException - If the resource has been released or closed.ThreadingIllegalStateException - If this resource was accessed by multiple threads in an unsafe waypublic void release(ReferenceOwner id) throws ClosedIllegalStateException
When tracing is enabled, it checks the resource was an owner, and doesn't attempt to release twice.
release in interface ReferenceCountedid - The reference owner.ClosedIllegalStateException - If the resource has been released or closed.public void releaseLast(ReferenceOwner id) throws ClosedIllegalStateException
When tracing is enabled, it checks the resource was an owner, and doesn't attempt to release twice.
releaseLast in interface ReferenceCountedid - The reference owner.ClosedIllegalStateException - If the resource has been released or closed.public boolean tryReserve(ReferenceOwner id) throws ClosedIllegalStateException, IllegalArgumentException
tryReserve in interface ReferenceCountedid - The reference owner.true if the reservation was successful, false otherwise.ClosedIllegalStateException - If the resource has been released or closed.IllegalArgumentException - If the reference owner is not valid.public void reserveTransfer(ReferenceOwner from, ReferenceOwner to) throws ClosedIllegalStateException, ThreadingIllegalStateException
reserveTransfer in interface ReferenceCountedfrom - The current reference owner.to - The new reference owner.ClosedIllegalStateException - If the resource has been released or closed.ThreadingIllegalStateException - If this resource was accessed by multiple threads in an unsafe waypublic int refCount()
refCount in interface ReferenceCountedpublic void throwExceptionIfReleased()
throws ClosedIllegalStateException
throwExceptionIfReleased in interface ReferenceCountedTracerClosedIllegalStateException - If the resource has been released or closed.public void warnAndReleaseIfNotReleased()
throws IllegalStateException
warnAndReleaseIfNotReleased in interface ReferenceCountedTracerIllegalStateException - If the resource hadn't been released.public void singleThreadedCheckDisabled(boolean singleThreadedCheckDisabled)
SingleThreadedCheckedtrue, this resource can be shared between threads
as long as the users ensure that it is used in a thread-safe manner.singleThreadedCheckDisabled in interface SingleThreadedCheckedsingleThreadedCheckDisabled - true to turn off the thread safety check,
false to enable it.public void addReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
ReferenceCountedReferenceChangeListener that will be notified whenever the reference count changes.
This can be used to monitor the usage of the resource and execute code when certain conditions are met.addReferenceChangeListener in interface ReferenceCountedreferenceChangeListener - The listener that will receive notifications of reference count changes.public void removeReferenceChangeListener(ReferenceChangeListener referenceChangeListener)
ReferenceCountedReferenceChangeListener previously added via ReferenceCounted.addReferenceChangeListener(ReferenceChangeListener).
Note: Object equality is used to determine which listener to remove, so be cautious if the listener implements the equals method in a non-standard way.
removeReferenceChangeListener in interface ReferenceCountedreferenceChangeListener - The listener to remove.protected boolean threadSafetyCheck(boolean isUsed)
throws ThreadingIllegalStateException
ThreadingIllegalStateExceptionpublic void singleThreadedCheckReset()
singleThreadedCheckReset in interface SingleThreadedChecked@NotNull public @NotNull String toString()
public void referenceCountedUnmonitored(boolean unmonitored)
public void unmonitor()
MonitorableImplementations of this method should ensure that the resource and any resources it uses are no longer being tracked for any purpose such as cleanup, resource management, or debugging. This is particularly important for resources that are explicitly managed to avoid leaks.
unmonitor in interface MonitorableCopyright © 2024. All rights reserved.