7

I have a multithreaded application and a singleton class:

public final class Singleton {

    private static MyClass mc;

    public static final Object getInstance() {
            if(mc == null) {
                mc = new MyClass();
            }
            return mc;
    }

}

Of course, this will not work in a general multithreaded scenario. But consider the following scenario:

  • In the beginning there is only one thread
  • This one thread calls getInstance() for the first time so that mc is initialized.
  • After that all other threads are started by the first thread.

My assumption:

This should work because the initialization of the mc field and the object's construction happen-before all subsequent Thread.start() calls that start the other threads. And the Thread.start() for a thread happens-before all other actions of that thread. It follows that the initialization of mc happens-before all actions in all other threads so that getInstance() will return the right value for all threads.

Is this assumption right? Why / Why not?

9
  • Well, @StephenC, or I SHOULD ask people who (hopefully) understand the JMM and thus can EXPLAIN it to me. Nov 6, 2014 at 22:02
  • It'll be fine. It's called "eager initialization" (well essentially, although your form is a bit different, that is ultimately what you are doing). Google it. See also journaldev.com/1377/…
    – Jason C
    Nov 6, 2014 at 22:03
  • Yes it works but it is very fragile as you can imagine. And the cost of making it right, performance wise, is negligible.
    – assylias
    Nov 6, 2014 at 22:07
  • 2
    The thread safety depends on the caller respecting your workflow as opposed to being guaranteed by the design of the class.
    – assylias
    Nov 6, 2014 at 22:11
  • 2
    You could simply use final MyClass singleton = new MyClass(); it's eager and perfectly threadsafe - en.wikipedia.org/wiki/Singleton_pattern#Eager_initialization - lazy initialization has only benefits if there are chances that this class is just partially required long before you'll need the instance. In most cases, class loading (=static initialization) is the exact same time you need the singleton.
    – zapl
    Nov 6, 2014 at 22:14

1 Answer 1

5

Your analysis is indeed perfectly fine.

To be precise: Everything that happens on one thread sequentially has a happens before relationship (obviously, the says it this way: "If x and y are actions of the same thread and x comes before y in program order, then hb(x, y)." )

17.4.5 of the JLS then goes on to say the following:

"A call to start() on a thread happens-before any actions in the started thread."

So taken together there's a clear happens before order between the singleton instantiation and the started threads, hence they're guaranteed to see the up to date value.

Simply said: A created thread is guaranteed to see everything its parent did before it was created, otherwise concurrent programming would be nigh impossible.

3
  • I can confirm that @Voo is 100% correct. I've been doing just that for over a decade now with no problems.
    – edharned
    Nov 6, 2014 at 23:08
  • @StephenC The reasoning is based on the rules of the JMM about happens before relations and when they exist between two actions. What would be the better reasoning in your opinion? Nov 7, 2014 at 6:02
  • @Stephen The given explanation by the OP seems pretty close to what I have, dieting that you can establish a hb sequence between the actions. Which yes, seems perfectly fine to me.
    – Voo
    Nov 7, 2014 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.