JavaMemoryModel: Singleton question - and not about DCL!

From: Jon Skeet (
Date: Tue May 20 2003 - 02:59:13 EDT

I've been consider the most commonly used singleton pattern in Java recently. That is:

public class Singleton
    private static Singleton instance = new Singleton();

    // Some data here...
    private int i=5;

    private Singleton()

    public static Singleton getInstance()
        return instance;

Now, I'm quite well aware of how this works in terms of making sure that only one thread creates an instance, but I'm not clear on how it manages (if it manages!) to make sure that all threads can see "i" here. Consider: thread A creates the singleton at some stage, through calling getInstance which performs class initialization. I presume that in the process of unlocking the class here as initialized, there's a memory write barrier it passes through. Some time later, however, thread B calls getInstance. Does it have a memory read barrier to pass through? If not, can it guarantee to see the "correct" values either of instance itself, or of i within instance? If so, is there an implicit memory read barrier on *every* static method call? Or is it only the first static method call to any particular class from any particular thread?

I'm probably missing something really simple here...


JavaMemoryModel mailing list -

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