java的IntegerCache注释中的第一阶段是什么?

调试Java虽然可以看到Integer的源码,但有些代码却跟踪不到。比如:
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage.  The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* jdk.internal.misc.VM class.
* 这里的The cache is initialized on first usage是什么意思,第一阶段是那个阶段?
*/

private static class IntegerCache {
}


这里的第一阶段说的是jvm初始化的过程的一个阶段。看java代码是看出来的,需要查jvm的C++代码。下面的代码来自openjdk10,thread.cpp的Threads::initialize_java_lang_classes方法:

// The VM creates objects of this class.
initialize_class(vmSymbols::java_lang_Module(), CHECK);

// The VM preresolves methods to these classes. Make sure that they get initialized
initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK);

// Phase 1 of the system initialization in the library, java.lang.System class initialization
call_initPhase1(CHECK);

// get the Java runtime name after java.lang.System is initialized
JDK_Version::set_runtime_name(get_java_runtime_name(THREAD));


调试可以发现,在上面的call_initPhase1就会执行java.lang.Integer.IntegerCache的初始化。
我的c++功力比较弱,跟了一下发现上面的initialize就是加载字节码(class)文件,而这里的call_initPhase1就是让其执行java代码的初始化工作,但这里的java代码的初始化代码无法打断点,前面介绍了加System.out.println来观察其执行过程。


文/中中 浏览次数:0次   2020-04-02 18:57:45

相关阅读


评论: