2

I want to add the following code to my class:

static private final ILogic_P logicInstanceI =
    (ILogic_P)Factory.CreateAnon("some.path.ILogic_P" + (SomeClass.isIMDB() ? "1" : "2"));

public static ILogic_P getLogicInstanceI(){
    return logicInstanceI;
}

I can't figure it out if the initialization of the static variable is thread safety or not. Is there a chance that two threads will try to initialize this attribute simultaneously?

4
  • 5
    static instances are initialized during class loading and are "synchronized" per-class instance. so yes, this is thread safe. Commented Dec 9, 2015 at 15:34
  • I don't like that duplicate. Bah. Commented Dec 9, 2015 at 15:39
  • This one is better. Commented Dec 9, 2015 at 15:40
  • thanks, it is clear now. Commented Dec 10, 2015 at 15:30

1 Answer 1

5

The answer is given by the Java Language Specification §12.4.2:

Because the Java programming language is multithreaded, initialization of a class or interface requires careful synchronization, since some other thread may be trying to initialize the same class or interface at the same time. There is also the possibility that initialization of a class or interface may be requested recursively as part of the initialization of that class or interface; for example, a variable initializer in class A might invoke a method of an unrelated class B, which might in turn invoke a method of class A. The implementation of the Java Virtual Machine is responsible for taking care of synchronization and recursive initialization by using the following procedure. […]

Note the last sentence starting with “The implementation of the Java Virtual Machine is responsible for taking care …”

So you are not responsible for synchronization in the case of class initialization, and assigning initial values to static variables is part of the class initialization, as specified in §8.3.2:

8.3.2. Field Initialization

If a declarator in a field declaration has a variable initializer, then the declarator has the semantics of an assignment (§15.26) to the declared variable.

If the declarator is for a class variable (that is, a static field), then the following rules apply to its initializer:

  • At run time, the initializer is evaluated and the assignment performed exactly once, when the class is initialized (§12.4.2).
Sign up to request clarification or add additional context in comments.

2 Comments

This does, of course, assume the JVM's synchronization does not have any bugs. (Usually a safe assumption, but it's not as though compilers and virtual machines are guaranteed to be bug-free.) There's always some corner case somewhere that nobody ended up testing until a bug report comes in.
@JAB: if you assume a buggy JVM, you’re lost anyway. Who says that synchronized or whatever you use for thread safety will work correctly? But correct class initialization is one of the most crucial things about which JVM developers have to care…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.