In a multi-threaded environment, when multiple threads refer to a class at the same time, does the JVM end up loading the class multiple times?
If not, how does synchronization happen?
The class will be loaded once. See 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.
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With