On 27/09/2009, Stephen Colebourne <scolebou...@btopenworld.com> wrote: > > > > > Is the temporary variable "result" needed? > > > > > > Or maybe I'm missing some subtlety here ? > > > In which case it should be documented in a comment please. > > > > > > <snip/> > > > > > > > > > > According to Bloch's book the temporary variable is indeed a performance > optimization. It prevents that the volatile field is read twice in the > common case that the object has already been initialized. > > > > Its not about performance. Without the local variable, the pattern is > unsafe concurrently. The pattern must be followed exactly (including the > double assign) in order for the locking to be concurrent-safe.
Where is that described? The first reference I found says that the local variable is a performance optimisation; it's the volatile keyword that makes it thread-safe: http://technology.amis.nl/blog/4384/the-double-checked-locking-confusion also http://jeremymanson.blogspot.com/2008/05/double-checked-locking.html But of course these could be wrong... > Stephen > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org