public abstract class AbstractCloseable extends Object implements ReferenceOwner, ManagedCloseable, SingleThreadedChecked, Monitorable
The class provides base functionalities for resources that require proper management, especially when being closed. This includes ensuring that close operations are thread-safe, providing hooks for custom cleanup logic, and supporting diagnostic features for tracking resource usage. It supports resource tracing, thread safety checks, and ensures that close operations are performed properly.
The AbstractCloseable class implements the ReferenceOwner, ManagedCloseable, and SingleThreadedChecked interfaces.
The ReferenceOwner interface allows the class to have a unique reference identifier.
The ManagedCloseable interface ensures that this class provides mechanisms for proper resource management during close operations.
The SingleThreadedChecked interface ensures that the close operation is executed in a thread-safe manner.
Implementations of this abstract class should override the performClose() method to include the specific
cleanup logic needed for the resource. This ensures that custom cleanup logic is executed exactly once during the
closing of the resource.
Additionally, AbstractCloseable supports resource tracing, which can be enabled or disabled to monitor and
diagnose resource allocation and deallocation. Resource tracing can help in identifying resource leaks and ensure
resources are properly managed.
Subclasses can also control the behavior of thread safety checks and background closing through provided methods.
| Modifier and Type | Field and Description |
|---|---|
protected StackTrace |
closedHere |
protected static boolean |
DISABLE_DISCARD_WARNING
Flag indicating whether discard warning is disabled.
|
protected static long |
WARN_NS
The number of nanoseconds to warn before closing a resource.
|
INIT, TMPDISABLE_SINGLE_THREADED_CHECK| Modifier | Constructor and Description |
|---|---|
protected |
AbstractCloseable()
Constructs a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
assertCloseable()
Asserts that this resource can be closed, throwing an
IllegalStateException if it cannot. |
static void |
assertCloseablesClosed()
Asserts that all closeable resources are closed.
|
void |
close()
Closes this resource and releases any associated system resources.
|
StackTrace |
createdHere()
Returns the stack trace at the point this closeable resource was created.
|
static void |
disableCloseableTracing()
Disables tracing of closeable resources.
|
static void |
enableCloseableTracing()
Enables tracing of closeable resources.
|
static void |
gcAndWaitForCloseablesToClose()
Performs garbage collection and waits for closeables to close.
|
boolean |
isClosed()
Checks if the resource is closed.
|
boolean |
isClosing()
Checks if the resource is in the process of being closed.
|
protected boolean |
isInUserThread()
Checks if the current thread is a user thread.
|
protected abstract void |
performClose()
Contains the actual logic for closing the resource.
|
int |
referenceId()
Returns the unique reference ID of this closeable resource.
|
protected boolean |
shouldPerformCloseInBackground()
Determines if this component should be closed asynchronously in the background.
|
protected boolean |
shouldWaitForClosed()
Determines if this component should wait for the close operation to complete before
returning from the
close() method. |
protected boolean |
singleThreadedCheckDisabled()
Determines if the single-threaded safety check is disabled for this component.
|
void |
singleThreadedCheckDisabled(boolean singleThreadedCheckDisabled)
Disables or enables the single-threaded safety check.
|
void |
singleThreadedCheckReset()
Resets the state of the thread safety check.
|
protected void |
threadSafetyCheck(boolean isUsed)
Performs a thread safety check on the component.
|
void |
throwExceptionIfClosed()
Throws an exception if the resource is closed.
|
void |
throwExceptionIfClosedInSetter()
Throws an exception if the resource is closed during a setter operation.
|
String |
toString()
Returns a string representation of this component, typically used for debugging purposes.
|
void |
unmonitor()
Stops monitoring the resource.
|
static boolean |
waitForCloseablesToClose(long millis)
Waits for closeable resources to be closed within a specified timeout period.
|
protected void |
waitForClosed()
Waits for the resource to transition to the closed state.
|
void |
warnAndCloseIfNotClosed()
Checks if the resource is not closed, and if so, logs a warning and closes it.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitreferenceName, temporarycloseQuietly, closeQuietlyunmonitorprotected static final boolean DISABLE_DISCARD_WARNING
protected static final long WARN_NS
protected transient volatile StackTrace closedHere
protected AbstractCloseable()
public static void enableCloseableTracing()
public static void disableCloseableTracing()
public static void gcAndWaitForCloseablesToClose()
NOTE: This is slower than waitForCloseablesToClose and it can clean up resources that should have be released deterministically.
AssertionError - If the finalizer does not complete within the specified timeout.public static boolean waitForCloseablesToClose(long millis)
millis - the maximum time in milliseconds to wait for closeables to close.true if all closeable resources were closed within the specified
timeout, false otherwise.public static void assertCloseablesClosed()
AssertionError.AssertionError - if any closeable resource is not closed.public int referenceId()
referenceId in interface ReferenceOwnerpublic StackTrace createdHere()
createdHere in interface ManagedCloseablepublic final void close()
close in interface Closeableclose in interface AutoCloseableperformClose()protected void assertCloseable()
IllegalStateException if it cannot.
This can be useful for resources that have outstanding guarantees such as acquired update locks or open contexts.
Electing to throw an Exception might cause memory-leaks as there is no guarantee that the close methods will ever be invoked again in the general case.
IllegalStateException - if the resource cannot be closed.protected boolean isInUserThread()
true if the current thread is a user thread; false otherwise.protected void waitForClosed()
public void throwExceptionIfClosed()
throws ClosedIllegalStateException,
ThreadingIllegalStateException
throwExceptionIfClosed in interface ManagedCloseableClosedIllegalStateException - If the resource has been released or closed.ThreadingIllegalStateException - If the thread safety check fails.public void throwExceptionIfClosedInSetter()
throws ClosedIllegalStateException,
ThreadingIllegalStateException
ClosedIllegalStateException - If the resource has been released or closed.ThreadingIllegalStateException - If the thread safety check fails.public void warnAndCloseIfNotClosed()
Called from finalise() implementations.
warnAndCloseIfNotClosed in interface ManagedCloseableprotected abstract void performClose()
Note: This method is called exactly once through callPerformClose().
public boolean isClosing()
isClosing in interface QueryCloseabletrue if the resource is either closed or in the process of being closed;
false otherwise.public boolean isClosed()
isClosed in interface QueryCloseabletrue if the resource is closed; false otherwise.protected boolean shouldPerformCloseInBackground()
true if this component should be closed in the background; false otherwise.protected boolean shouldWaitForClosed()
close() method.
Subclasses may override this method to change the default behavior.true if this component should wait for the close to complete; false otherwise.protected void threadSafetyCheck(boolean isUsed)
throws ThreadingIllegalStateException
ThreadingIllegalStateException is thrown.
This method is intended to be called before operations that require thread safety.isUsed - flag indicating whether the component is being used in the current operation.ThreadingIllegalStateException - If the component is not thread-safe and accessed by multiple threads.public void singleThreadedCheckReset()
singleThreadedCheckReset in interface SingleThreadedCheckedpublic String toString()
protected boolean singleThreadedCheckDisabled()
true if single-threaded safety check is disabled; false otherwise.public void singleThreadedCheckDisabled(boolean singleThreadedCheckDisabled)
singleThreadedCheckDisabled in interface SingleThreadedCheckedsingleThreadedCheckDisabled - true to disable single-threaded safety check; false to enable it.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.