gpt4 book ai didi

java - 当多个线程同时请求加载同一个类时会发生什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:23:47 24 4
gpt4 key购买 nike

在多线程环境下,当多个线程同时引用一个类时,JVM 是否最终会多次加载该类?

如果不是,同步是如何发生的?

最佳答案

该类将被加载一次。参见 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:

  1. Synchronize on the initialization lock, LC, for C. This involves waiting until the current thread can acquire LC.

...

关于java - 当多个线程同时请求加载同一个类时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55204559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com