public abstract class AbstractCloseable extends Object implements ReferenceOwner, ManagedCloseable, SingleThreadedChecked, Monitorable
ManagedCloseable and performs thread-safety checks.
Concrete subclasses implement their clean-up in performClose().
Resource tracing and optional background closing are provided for debugging
and performance tuning. See Closeable.closeQuietly(Object) for a
helper that ignores exceptions from performClose().
| 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()
Idempotent close entry point used by try-with-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()
Implement the clean-up logic.
|
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()
If the resource is still open a warning is logged and it is closed using
Closeable.closeQuietly(Object). |
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()
performClose().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()
Closeable.closeQuietly(Object). Intended for use from finalisers.warnAndCloseIfNotClosed in interface ManagedCloseableprotected abstract void performClose()
close() invokes this method exactly
once via 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 © 2026 Chronicle Software Ltd. All rights reserved.