[concurrency-interest] Thread safety of WeakReference .get() method?

Vitaly Davidovich vitalyd at gmail.com
Tue Aug 7 18:13:11 EDT 2012


I agree with this.  However, another interesting scenario is if thread 1
sets the referent and then publishes the WeakReference via a data race, can
thread 2 see the WeakRef as non-null but the referent as null or partially
initialized? Referent is not final and I don't remember seeing any other
barriers that can be piggybacked on in the constructor.  Unless the JVM
does something internally, the JMM would allow this to happen.

Cheers,
Vitaly

Sent from my phone
On Aug 7, 2012 9:54 AM, "Aleksey Shipilev" <aleksey.shipilev at oracle.com>
wrote:

> On 08/07/2012 04:47 PM, Raph Frank wrote:
>
> > The documentation says that once an object has been determined to be
> > garbage collectable, "At that time it will atomically clear all weak
> > references to that object", so presumably, the get method can be
> > called, some if is atomically set to null?
>
> I think the real meaning behind that claim is "atomically set *all* weak
> references to that object", i.e. two weak references to the same object
> will simultaneously become clear once object is not reachable.
>
> Mutator threads are not modifying the referent, and so the real question
> should be: is it possible to read the non-null referent after weak
> reference is cleared? There is no difference how many user threads are
> accessing now, because the possible race is between mutator threads and GC.
>
> The answer to that is VM-specific. Common sense is that all GC
> operations should be committed before returning back to the mutator
> after stop-the-world, or notified to the mutator as soon as possible in
> concurrent GCs. If that wasn't true, WeakRefs would risk keep
> referencing the objects that are not really there, and it would broke
> too many applications already.
>
> -Aleksey.
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120807/6b70e36a/attachment.html>


More information about the Concurrency-interest mailing list