lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <...@thetaphi.de>
Subject RE: Immutable OpenBitSet?
Date Thu, 28 Apr 2011 14:05:18 GMT
The missing safe publish of the reference to the object is the problem here. Once you have
published "f" safely between writer and reader, all contents and references to other objects
inside the FinalFieldExample are visible.

The final modifier on this only assures in the case of no safe publish, that if for some reason
the reader actually sees the object reference f!=null you then see the value of f.x. But as
this is broken, you would never do this - the reader could even simply see null. Because of
this you have to safe publish f, and then also f.x and f.x are correct (and then it does not
matter if final or not).

To fix the example, make reader and writer synchronized, then f is published in a safe way
(alternatively make f volatile). And that is what e.g. CachingWrapperFilter does (it puts
the OpenBitSet into a synchronized map) - see my last mail.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Dawid Weiss [mailto:dawid.weiss@gmail.com]
> Sent: Thursday, April 28, 2011 3:00 PM
> To: java-user@lucene.apache.org
> Subject: Re: Immutable OpenBitSet?
> 
> >      static void writer() {
> >        f = new FinalFieldExample();
> >      }
> >      static void reader() {
> >        if (f != null) {
> >          int i = f.x; // guaranteed to see 3
> >          int j = f.y; // could see 0
> >        }
> >      }
> >    }
> >
> 
> In this snippet of code there's not even a guarantee that reader() will see the
> change to field f... I used to create puzzlers for students around spin loops
> that got optimized away by hotspot to never again reference the
> (condition) field because the code never mutated it inside the loop. So
> something like this:
> 
> while (f == null) {}
> 
> should get jitted into this:
> 
> while (true) {}
> 
> D.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message