java.lang.Object
java.lang.ref.Reference<R>
java.lang.ref.WeakReference<R>
org.microbean.bean.DisposableReference<R>
- Type Parameters:
R
- the type of the reference
- All Implemented Interfaces:
AutoCloseable
An
AutoCloseable
WeakReference
that formally disposes of referents after they have been cleared by the Java Virtual Machine during garbage collection.
Note that garbage collection may never happen.
- Author:
- Laird Nelson
- See Also:
-
Constructor Summary
ConstructorDescriptionDisposableReference
(R referent, Consumer<? super R> disposer) Creates a newDisposableReference
. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
close()
CallsReference.enqueue()
.final boolean
closed()
CallsrefersTo(null)
and returns the result.final boolean
dispose()
If there has been no prior successful invocation of this method, calls theaccept(Object)
method on theConsumer
representing the disposer supplied at construction time, thus notionally disposeing the referent supplied at construction time, and returnstrue
.final boolean
disposed()
Returnstrue
if and only if there has been a prior successful invocation ofdispose()
that returnedtrue
.Methods inherited from class java.lang.ref.Reference
clear, clone, enqueue, get, isEnqueued, reachabilityFence, refersTo
-
Constructor Details
-
DisposableReference
Creates a newDisposableReference
.- Parameters:
referent
- the referent; may benull
disposer
- a thread-safeConsumer
whoseaccept(Object)
method, which must be idempotent, will be invoked from a separate thread to dispose of the referent after it has been cleared by the Java Virtual Machine during garbage collection; may benull
in which case no destruction will take place
-
-
Method Details
-
close
CallsReference.enqueue()
.- Specified by:
close
in interfaceAutoCloseable
- See Also:
-
closed
CallsrefersTo(null)
and returns the result.- Returns:
- the result of an invocation of
refersTo(null)
- See Also:
-
dispose
If there has been no prior successful invocation of this method, calls theaccept(Object)
method on theConsumer
representing the disposer supplied at construction time, thus notionally disposeing the referent supplied at construction time, and returnstrue
.Destruction does not imply closing, and closing does not imply destruction (though it normally will eventually lead to it).
If destruction does not result in any
RuntimeException
orError
being thrown, then calling this method again, from any thread, will have no effect and it will returnfalse
.If the first invocation of this method from any thread succeeds, then it will return
true
, and all other invocations of this method from any thread will returnfalse
, and will have no effect.This method is often called from a thread dedicated to disposeing enqueued
DisposableReference
s, so theConsumer
supplied at construction time must be thread-safe.- Returns:
true
if this invocation of this method caused destruction to happen successfully;false
in all other cases- Throws:
RuntimeException
- if an invocation of theaccept(Object)
method on theConsumer
supplied at construction time fails- See Also:
- Idempotency:
- This method is idempotent.
- Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
disposed
Returnstrue
if and only if there has been a prior successful invocation ofdispose()
that returnedtrue
.- Returns:
true
if and only if there has been a prior successful invocation ofdispose()
that returnedtrue
- See Also:
-