Class CleanupReferenceProcessor
CleanupReferences and invoking their cleanup methods.-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCleanupReferenceProcessor(@NotNull String name, long shutdownCheckDelayMillis, @NotNull CleanupReferenceProcessor.ExceptionHandler exceptionHandler) Construct a newCleanupReferenceProcessor. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull CleanupReferenceProcessor<RT> ReferenceQueue<RT>Get the reference queue for this cleaner.<T> CleanupReference<T>registerPhantom(T referent, Runnable action) Registers areferentand a cleaningactionto run when thereferentbecomes phantom reachable.<T> CleanupReference<T>registerSoft(T referent, Runnable action) Registers areferentand a cleaningactionto run when thereferentbecomes softly reachable.<T> CleanupReference<T>registerWeak(T referent, Runnable action) Registers areferentand a cleaningactionto run when thereferentbecomes weakly reachable.final voidReset this instance so that the next call togetReferenceQueue()will re-initialize it and provide a new queue.
-
Constructor Details
-
CleanupReferenceProcessor
public CleanupReferenceProcessor(@NotNull @NotNull String name, long shutdownCheckDelayMillis, @NotNull @NotNull CleanupReferenceProcessor.ExceptionHandler exceptionHandler) Construct a newCleanupReferenceProcessor.- Parameters:
name- The name of the processor, used for naming threadsshutdownCheckDelayMillis- The frequency with which to check for shutdownexceptionHandler- Callback for exception handling
-
-
Method Details
-
getDefault
-
getReferenceQueue
Get the reference queue for this cleaner.
On the first call after construction or
resetForUnitTests(), this method initializes the instance as a side effect. Initialization entails:- Constructing a
ReferenceQueue. - Starting a daemon thread that will drain the reference queue and invoke
CleanupReference.cleanup()on anyCleanupReferencedequeued.
- Returns:
- The
ReferenceQueueconstructed in the most recent initialization of thisCleanupReferenceProcessorinstance
- Constructing a
-
registerPhantom
Registers areferentand a cleaningactionto run when thereferentbecomes phantom reachable.The most efficient use is to explicitly invoke the
cleanupmethod when thereferentis closed or no longer needed. Otherwise, the cleaningactionwill be invoked whenreferenthas become phantom reachable. Theactionwill not be invoked more than once.The cleaning
actionmust not refer to thereferentbeing registered. If so, thereferentwill never become phantom reachable and the cleaningactionwill never be invoked automatically.Note: while the caller is encouraged to hold onto the cleanup reference to allow for explicit
cleanupinvocation, they are not required to as this cleanup reference processor will hold onto the reference.- Parameters:
referent- the object to monitoraction- aRunnableto invoke when the referent becomes phantom reachable- Returns:
- a cleanup reference instance
-
registerWeak
Registers areferentand a cleaningactionto run when thereferentbecomes weakly reachable.The most efficient use is to explicitly invoke the
cleanupmethod when thereferentis closed or no longer needed. Otherwise, the cleaningactionwill be invoked whenreferenthas become weakly reachable. Theactionwill not be invoked more than once.The cleaning
actionmust not refer to thereferentbeing registered. If so, thereferentwill never become weakly reachable and the cleaningactionwill never be invoked automatically.Note: while the caller is encouraged to hold onto the cleanup reference to allow for explicit
cleanupinvocation, they are not required to as this cleanup reference processor will hold onto the reference.- Parameters:
referent- the object to monitoraction- aRunnableto invoke when the referent becomes weakly reachable- Returns:
- a cleanup reference instance
-
registerSoft
Registers areferentand a cleaningactionto run when thereferentbecomes softly reachable.The most efficient use is to explicitly invoke the
cleanupmethod when thereferentis closed or no longer needed. Otherwise, the cleaningactionwill be invoked whenreferenthas become softly reachable. Theactionwill not be invoked more than once.The cleaning
actionmust not refer to thereferentbeing registered. If so, thereferentwill never become softly reachable and the cleaningactionwill never be invoked automatically.Note: while the caller is encouraged to hold onto the cleanup reference to allow for explicit
cleanupinvocation, they are not required to as this cleanup reference processor will hold onto the reference.- Parameters:
referent- the object to monitoraction- aRunnableto invoke when the referent becomes softly reachable- Returns:
- a cleanup reference instance
-
resetForUnitTests
Reset this instance so that the next call togetReferenceQueue()will re-initialize it and provide a new queue. Results in the prompt termination of the daemon thread that may have been draining the existing queue.
-