RE: JavaMemoryModel: Will this solution work?

From: Srinivas_Kunani@toyota.com
Date: Wed Sep 17 2003 - 17:12:28 EDT


Evan:

I am not aware of some of the advanced topics being discussed on this
alias with respect to Java Memory Model. Pardon me if my explanation is
naive.

The reasoning for not writing the code as follows is

> ...
> private static Singleton instance;
> private static Object mutex = new Object();
> private static boolean initialized = false;
>
> private Singleton() {
> }
>
> public static Singleton getInstance() {
> if (instance == null || ! initialized) {
> synchronized(mutex) {
> if (instance == null ) {
> instance = new Singleton(); //Line A
> initialized = true; //Line B
> }
> }
> }
> return instance;
> }
> ...

Based on what I read about compiler optimizations, it is possible that
Line B and Line A can be executed out of sequence. Hence I had to force
the initialized to set to true only after the Singleton object has been
fully constructed.

by using the following line instead of line B,

initialized = (instance.getClass() != null);

I am assuming that this prevent the compiler from reordering the statement
as the execution of instance.getClass will not be valid until it is fully
constructed. Therefore the setting of the initialized variable to true can
only occur after the Singleton object is fully constructed and instance
variable refers to the valid Singleton object.

Alternatively, line B could be replaced by the following line as well:

initialized = (((double) instance.hashCode()) != Double.NaN);

The approach being that instance.hashCode() cannot be validly computed
until the Singleton object is fully constructed.

Thanks,
Srinivas.

Please respond to <eireland@sybase.com>
Sent by: owner-javamemorymodel@cs.umd.edu
To: <Srinivas_Kunani@toyota.com>, <javaMemoryModel@cs.umd.edu>
cc:

Subject: RE: JavaMemoryModel: Will this solution work?

Srinivas,

I could appreciate why the condition (instance == null || ! initialized)
might be helpful, but was not clear on:

    initialized = (instance.getClass() != null);

Why did you feel that:

    initialized = true;

would not be sufficient.

> -----Original Message-----
> From: owner-javamemorymodel@cs.umd.edu
> [mailto:owner-javamemorymodel@cs.umd.edu]On Behalf Of
> Srinivas_Kunani@toyota.com
> Sent: Wednesday, 17 September 2003 3:49 p.m.
> To: javaMemoryModel@cs.umd.edu
> Subject: JavaMemoryModel: Will this solution work?
>
>
> I am trying to get an understanding of the subtleties of the
Double-Check
> locking problem in Java
>
> Will the following code resolve the problem. If it will not, can
somebody
> please explain why not.
>
> Thanks in advance for your responses.
>
> Srinivas.
>
> ...
> private static Singleton instance;
> private static Object mutex = new Object();
> private static boolean initialized = false;
>
> private Singleton() {
> }
>
> public static Singleton getInstance() {
> if (instance == null || ! initialized) {
> synchronized(mutex) {
> if (instance == null ) {
> instance = new Singleton();
> initialized = (instance.getClass() != null);
> }
> }
> }
> return instance;
> }
> ...
>

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

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



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