Java關(guān)于JIT的原理和相關(guān)知識博客分類(lèi):
Java2DJavaJVMSecuritySUNAccess今天在讀java.awt.Toolkit類(lèi)時(shí),遇到了JIT,代碼如下:
Java代碼
/**
* Gets the default toolkit.
* <p>
* If a system property named <code>"java.awt.headless"</code> is set
* to <code>true</code> then the headless implementation
* of <code>Toolkit</code> is used.
* <p>
* If there is no <code>"java.awt.headless"</code> or it is set to
* <code>false</code> and there is a system property named
* <code>"awt.toolkit"</code>,
* that property is treated as the name of a class that is a subclass
* of <code>Toolkit</code>;
* otherwise the default platform-specific implementation of
* <code>Toolkit</code> is used.
* <p>
* Also loads additional classes into the VM, using the property
* 'assistive_technologies' specified in the Sun reference
* implementation by a line in the 'accessibility.properties'
* file. The form is "assistive_technologies=..." where
* the "..." is a comma-separated list of assistive technology
* classes to load. Each class is loaded in the order given
* and a single instance of each is created using
* Class.forName(class).newInstance(). This is done just after
* the AWT toolkit is created. All errors are handled via an
* AWTError exception.
* @return the default toolkit.
* @exception AWTError if a toolkit could not be found, or
* if one could not be accessed or instantiated.
*/
public static synchronized Toolkit getDefaultToolkit() {
if (toolkit == null) {
try {
// We disable the JIT during toolkit initialization. This
// tends to touch lots of classes that aren't needed again
// later and therefore JITing is counter-productiive.
java.lang.Compiler.disable();
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
String nm = null;
Class cls = null;
try {
nm = System.getProperty("awt.toolkit", "sun.awt.X11.XToolkit");
try {
cls = Class.forName(nm);
} catch (ClassNotFoundException e) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
try {
cls = cl.loadClass(nm);
} catch (ClassNotFoundException ee) {
throw new AWTError("Toolkit not found: " + nm);
}
}
}
if (cls != null) {
toolkit = (Toolkit)cls.newInstance();
if (GraphicsEnvironment.isHeadless()) {
toolkit = new HeadlessToolkit(toolkit);
}
}
} catch (InstantiationException e) {
throw new AWTError("Could not instantiate Toolkit: " + nm);
} catch (IllegalAccessException e) {
throw new AWTError("Could not access Toolkit: " + nm);
}
return null;
}
});
loadAssistiveTechnologies();
} finally {
// Make sure to always re-enable the JIT.
java.lang.Compiler.enable();
}
}
return toolkit;
}
1.JIT的工作原理圖
工作原理
當JIT編譯啟用時(shí)(默認是啟用的),JVM讀入.class文件解釋后,將其發(fā)給JIT編譯器。JIT編譯器將字節碼編譯成本機機器代碼。
通常javac將程序源代碼編譯,轉換成java字節碼,JVM通過(guò)解釋字節碼將其翻譯成對應的機器指令,逐條讀入,逐條解釋翻譯。很顯然,經(jīng)過(guò)解釋執行,其執行速度必然會(huì )比可執行的二進(jìn)制字節碼程序慢。為了提高執行速度,引入了JIT技術(shù)。
在運行時(shí)JIT會(huì )把翻譯過(guò)的機器碼保存起來(lái),已備下次使用,因此從理論上來(lái)說(shuō),采用該JIT技術(shù)可以,可以接近以前純編譯技術(shù)。
2.相關(guān)知識
JIT是just in time,即時(shí)編譯技術(shù)。使用該技術(shù),能夠加速java程序的執行速度。
JIT并不總是奏效,不能期望JIT一定能夠加速你代碼執行的速度,更糟糕的是她有可能降低代碼的執行速度。這取決于你的代碼結構,當然很多情況下我們還是能夠如愿以?xún)數摹?div style="height:15px;">
從上面我們知道了之所以要關(guān)閉JITjava.lang.Compiler.disable(); 是因為加快執行的速度。由于JIT對每條字節碼都進(jìn)行編譯,造成了編譯過(guò)程負擔過(guò)重。為了避免這種情況,當前的JIT只對經(jīng)常執行的字節碼進(jìn)行編譯,如循環(huán)等。