多线程中声明期间的静态变量初始化
Static variable initialization during declaration in multi-threading
我想将以下代码添加到我的 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;
}
搞不懂静态变量的初始化是不是线程安全的。
是否有可能两个线程同时尝试初始化此属性?
答案由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. […]
注意最后一句以“Java虚拟机的实现负责照顾 ...”
所以在class初始化的情况下你不负责同步,给static
变量赋初值是class初始化的一部分,在§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).
我想将以下代码添加到我的 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;
}
搞不懂静态变量的初始化是不是线程安全的。 是否有可能两个线程同时尝试初始化此属性?
答案由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 classB
, which might in turn invoke a method of classA
. The implementation of the Java Virtual Machine is responsible for taking care of synchronization and recursive initialization by using the following procedure. […]
注意最后一句以“Java虚拟机的实现负责照顾 ...”
所以在class初始化的情况下你不负责同步,给static
变量赋初值是class初始化的一部分,在§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).