public abstract class AbstractCloseable extends Object implements ReferenceOwner, ManagedCloseable, SingleThreadedChecked
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 boolean |
DISABLE_THREAD_SAFETY
Deprecated.
|
protected static boolean |
STRICT_DISCARD_WARNING
Deprecated.
|
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
AbstractCloseable 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 |
clearUsedByThread()
Deprecated.
Use @{code singleThreadedCheckReset()} instead
|
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.
|
boolean |
disableThreadSafetyCheck()
Deprecated.
Use @{code singleThreadedCheckDisabled()} instead
|
AbstractCloseable |
disableThreadSafetyCheck(boolean disableThreadSafetyCheck)
Deprecated.
Use @{code disableThreadSafetyCheck(boolean)} instead
|
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.
|
void |
resetUsedByThread()
Deprecated.
Use @{code singleThreadedCheckReset()} instead
|
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.
|
static void |
unmonitor(Closeable closeable)
Removes the specified closeable resource from monitoring.
|
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, closeQuietly@Deprecated protected static final boolean DISABLE_THREAD_SAFETY
protected static final boolean DISABLE_DISCARD_WARNING
@Deprecated protected static final boolean STRICT_DISCARD_WARNING
protected static final long WARN_NS
protected transient volatile StackTrace closedHere
protected AbstractCloseable()
AbstractCloseable instance. Registers the instance
for resource tracing and monitoring if enabled.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 static void unmonitor(Closeable closeable)
closeable - the closeable resource to unmonitor.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.@Deprecated public void resetUsedByThread()
@Deprecated public void clearUsedByThread()
public void singleThreadedCheckReset()
singleThreadedCheckReset in interface SingleThreadedCheckedpublic String toString()
@Deprecated public boolean disableThreadSafetyCheck()
protected boolean singleThreadedCheckDisabled()
true if single-threaded safety check is disabled; false otherwise.@Deprecated public AbstractCloseable disableThreadSafetyCheck(boolean disableThreadSafetyCheck)
public void singleThreadedCheckDisabled(boolean singleThreadedCheckDisabled)
singleThreadedCheckDisabled in interface SingleThreadedCheckedsingleThreadedCheckDisabled - true to disable single-threaded safety check; false to enable it.Copyright © 2023. All rights reserved.