Class DisposableReference<R>
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
ConstructorsConstructorDescriptionDisposableReference(R referent, Consumer<? super R> disposer) Creates a newDisposableReference. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()CallsReference.enqueue().final booleanclosed()CallsrefersTo(null)and returns the result.final booleandispose()If there has been no prior successful invocation of this method, calls theaccept(Object)method on theConsumerrepresenting the disposer supplied at construction time, thus notionally disposing the referent supplied at construction time, and returnstrue.final booleandisposed()Returnstrueif 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 benulldisposer- a thread-safeConsumerwhoseaccept(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 benullin which case no destruction will take place
-
-
Method Details
-
close
CallsReference.enqueue().- Specified by:
closein 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 theConsumerrepresenting the disposer supplied at construction time, thus notionally disposing 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
RuntimeExceptionorErrorbeing 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 disposing enqueued
DisposableReferences, so theConsumersupplied at construction time must be thread-safe.- Returns:
trueif this invocation of this method caused destruction to happen successfully;falsein all other cases- Throws:
RuntimeException- if an invocation of theaccept(Object)method on theConsumersupplied at construction time fails- See Also:
- Idempotency:
- This method is idempotent.
- Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
disposed
-