Is Reference.reachabilityFence() needed in Reference constructor?
Peter Levart
peter.levart at gmail.com
Wed Oct 21 12:34:27 UTC 2015
Hi,
I have a question about a use-case for Reference.reachabilityFence().
While reviewing code of new Cleaner API that is being proposed on
core-libs-dev, the following java.lang.Reference constructor caught my eye:
Reference(T referent, ReferenceQueue<? super T> queue) {
this.referent = referent;
this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
}
Say it is being used like that:
ReferenceQueue<Object> queue = ...;
WeakReference<Object> wr = new WeakReference(new Object(), queue);
Is it possible that the newly constructed Object is found
weakly-reachable before the 'queue' is assigned to the Reference.queue
field or is there something in the JVM that prevents this happening
(like all the constructor parameters are reachable at least until the
constructor is finished)? Might the following be needed or not:
Reference(T referent, ReferenceQueue<? super T> queue) {
this.referent = referent;
this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
reachabilityFence(referent);
}
Regards, Peter
More information about the core-libs-dev
mailing list