Class CleanupReferenceProcessor
CleanupReference
s and invoking their cleanup methods.-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionCleanupReferenceProcessor
(@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 areferent
and a cleaningaction
to run when thereferent
becomes phantom reachable.<T> CleanupReference<T>
registerSoft
(T referent, Runnable action) Registers areferent
and a cleaningaction
to run when thereferent
becomes softly reachable.<T> CleanupReference<T>
registerWeak
(T referent, Runnable action) Registers areferent
and a cleaningaction
to run when thereferent
becomes weakly reachable.final void
Reset 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 anyCleanupReference
dequeued.
- Returns:
- The
ReferenceQueue
constructed in the most recent initialization of thisCleanupReferenceProcessor
instance
- Constructing a
-
registerPhantom
Registers areferent
and a cleaningaction
to run when thereferent
becomes phantom reachable.The most efficient use is to explicitly invoke the
cleanup
method when thereferent
is closed or no longer needed. Otherwise, the cleaningaction
will be invoked whenreferent
has become phantom reachable. Theaction
will not be invoked more than once.The cleaning
action
must not refer to thereferent
being registered. If so, thereferent
will never become phantom reachable and the cleaningaction
will never be invoked automatically.Note: while the caller is encouraged to hold onto the cleanup reference to allow for explicit
cleanup
invocation, they are not required to as this cleanup reference processor will hold onto the reference.- Parameters:
referent
- the object to monitoraction
- aRunnable
to invoke when the referent becomes phantom reachable- Returns:
- a cleanup reference instance
-
registerWeak
Registers areferent
and a cleaningaction
to run when thereferent
becomes weakly reachable.The most efficient use is to explicitly invoke the
cleanup
method when thereferent
is closed or no longer needed. Otherwise, the cleaningaction
will be invoked whenreferent
has become weakly reachable. Theaction
will not be invoked more than once.The cleaning
action
must not refer to thereferent
being registered. If so, thereferent
will never become weakly reachable and the cleaningaction
will never be invoked automatically.Note: while the caller is encouraged to hold onto the cleanup reference to allow for explicit
cleanup
invocation, they are not required to as this cleanup reference processor will hold onto the reference.- Parameters:
referent
- the object to monitoraction
- aRunnable
to invoke when the referent becomes weakly reachable- Returns:
- a cleanup reference instance
-
registerSoft
Registers areferent
and a cleaningaction
to run when thereferent
becomes softly reachable.The most efficient use is to explicitly invoke the
cleanup
method when thereferent
is closed or no longer needed. Otherwise, the cleaningaction
will be invoked whenreferent
has become softly reachable. Theaction
will not be invoked more than once.The cleaning
action
must not refer to thereferent
being registered. If so, thereferent
will never become softly reachable and the cleaningaction
will never be invoked automatically.Note: while the caller is encouraged to hold onto the cleanup reference to allow for explicit
cleanup
invocation, they are not required to as this cleanup reference processor will hold onto the reference.- Parameters:
referent
- the object to monitoraction
- aRunnable
to 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.
-