RE: JavaMemoryModel: Will this solution work?

From: Evan Ireland (eireland@sybase.com)
Date: Wed Sep 17 2003 - 00:22:02 EDT


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



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