Java关于JIT的原理和相关知识

今天在读java.awt.Toolkit类时,遇到了JIT,代码如下:
/**
* 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;
}

[b]1.JIT的工作原理图[/b]
[img]http://www.kutoku.info/images/java/100913/nancb4.eps.gif[/img]
工作原理
当JIT编译启用时(默认是启用的),JVM读入.class文件解释后,将其发给JIT编译器。JIT编译器将字节码编译成本机机器代码。

通常javac将程序源代码编译,转换成java字节码,JVM通过解释字节码将其翻译成对应的机器指令,逐条读入,逐条解释翻译。很显然,经过解释执行,其执行速度必然会比可执行的二进制字节码程序慢。为了提高执行速度,引入了JIT技术。

在运行时JIT会把翻译过的机器码保存起来,已备下次使用,因此从理论上来说,采用该JIT技术可以,可以接近以前纯编译技术。

[b]2.相关知识[/b]
JIT是just in time,即时编译技术。使用该技术,能够加速java程序的执行速度。

JIT并不总是奏效,不能期望JIT一定能够加速你代码执行的速度,更糟糕的是她有可能降低代码的执行速度。这取决于你的代码结构,当然很多情况下我们还是能够如愿以偿的。

从上面我们知道了之所以要关闭JITjava.lang.Compiler.disable(); 是因为加快执行的速度。由于JIT对每条字节码都进行编译,造成了编译过程负担过重。为了避免这种情况,当前的JIT只对经常执行的字节码进行编译,如循环等。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值