Re: JavaMemoryModel: Question about the semantics of volatile

From: Bill Pugh (pugh@cs.umd.edu)
Date: Wed Mar 17 2004 - 15:48:55 EST


On Mar 17, 2004, at 3:33 PM, Doron Rajwan wrote:

>
> When using volatile member for cache, when duplicate
> instances are unimportant, I would like to use the
> following pattern:
>
> private static volatile X instance;
>
> public static X get() {
> X temp = instance;
> if (temp != null)
> return temp;
> return instance = new X();
> }
>
> I do not want a second volatile read!
>
> The weak interpretation allows it. In the strong
> interpretation, it seems that I have to write the last
> line in 3 lines:
>
> temp = new X();
> instance = temp;
> return temp;
>
> It this is true, I prefer the weak interpretation.
>
> Doron.
>

Using the strong or weak interpretation wouldn't make any difference
for this example.

Bill

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:01:00 EDT