Class CleanableState
java.lang.Object
org.newsclub.net.unix.CleanableState
- All Implemented Interfaces:
Closeable, AutoCloseable
This wrapper (along with the Java 8-specific counterpart in src/main/java8) allows us to
implement cleanup logic for objects that are garbage-collectable/no longer reachable.
Usage:
- Create a subclass of CleanableState and attach it as a private field to the object you want
to be cleaned up. You may call that field
cleanableState. - Define all resources that need to be cleaned up in this subclass (instead of the observed object itself).
- Make sure to not refer the observed object instance itself, as that will create a reference cycle and prevent proper cleanup.
- Implement the
doClean()method to perform all the necessary cleanup steps. - If the observed class implements
close(), it's a good practice to have it just callcleanableState.runCleaner().
Exceptions thrown upon doClean() are either thrown via close() when invoked directly,
or, if cleaned during Garbage collection, to the exception handler specified in the constructor
or (if none specified) to the default uncaught exception handler.
Implementation details:
- In Java 9 or later,
Cleaneris used under the hood. - In Java 8 or earlier,
Object.finalize()callsdoClean()directly.
- Author:
- Christian Kohlschütter
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final AFConsumer<Throwable> A default exception handler: CallsThread.getDefaultUncaughtExceptionHandler()with the stacktrace and information about the current thread. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCleanableState(Object observed) Creates a state object to be used as an implementation detail of the specified observed instance, using theDEFAULT_EXCEPTION_HANDLER.protectedCleanableState(Object observed, AFConsumer<Throwable> exceptionHandler) Creates a state object to be used as an implementation detail of the specified observed instance, using a custom exception handler. -
Method Summary
-
Field Details
-
DEFAULT_EXCEPTION_HANDLER
A default exception handler: CallsThread.getDefaultUncaughtExceptionHandler()with the stacktrace and information about the current thread.
-
-
Constructor Details
-
CleanableState
Creates a state object to be used as an implementation detail of the specified observed instance, using theDEFAULT_EXCEPTION_HANDLER.- Parameters:
observed- The observed instance (the outer class referencing thisCleanableState).
-
CleanableState
Creates a state object to be used as an implementation detail of the specified observed instance, using a custom exception handler.- Parameters:
observed- The observed instance (the outer class referencing thisCleanableState).exceptionHandler- The exception handler.
-
-
Method Details
-
runCleaner
public final void runCleaner()Explicitly the cleanup code defined indoClean(). This is best be called from aclose()method in the observed class. -
doClean
Performs the actual cleanup. Be sure to always clean up whenever possible, either by tracking potential exceptions or by using try-finally to ensure proper cleanup.- Throws:
IOException- on error.
-
inClose
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-