当多个线程要求同时加载相同的 class 时会发生什么?
What happens when multiple threads ask for the same class to be loaded at same time?
在多线程环境中,当多个线程同时引用一个class时,JVM是否会多次加载class?
如果不是,同步是如何发生的?
class会加载一次。参见 jls 12.4.2
For each class or interface C, there is a unique initialization lock
LC. The mapping from C to LC is left to the discretion of the Java
Virtual Machine implementation. The procedure for initializing C is
then as follows:
- Synchronize on the initialization lock, LC, for C. This involves waiting until the current thread can acquire LC.
...
在多线程环境中,当多个线程同时引用一个class时,JVM是否会多次加载class?
如果不是,同步是如何发生的?
class会加载一次。参见 jls 12.4.2
For each class or interface C, there is a unique initialization lock LC. The mapping from C to LC is left to the discretion of the Java Virtual Machine implementation. The procedure for initializing C is then as follows:
- Synchronize on the initialization lock, LC, for C. This involves waiting until the current thread can acquire LC.
...