Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1347644
  • 博文数量: 205
  • 博客积分: 6732
  • 博客等级: 准将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-04 17:59
文章分类

全部博文(205)

文章存档

2016年(1)

2015年(10)

2014年(1)

2013年(39)

2012年(23)

2011年(27)

2010年(21)

2009年(55)

2008年(28)

我的朋友

分类: Java

2011-07-04 15:45:23


什么是JIT?

JIT是just in time,即时编译技术。使用该技术,能够加速java程序的执行速度。下面,就对该技术做个简单的讲解。

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

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

JIT 编译过程
当JIT编译启用时(默认是启用的),JVM读入.class文件解释后,将其发给JIT编译器。JIT编译器将字节码编译成本机机器代码,下图展示了该过程。



通过上面的解释,我们了解了JIT的工作原理及过程,同样也发现了个问题,由于JIT对每条字节码都进行编译,造成了编译过程负担过重。为了避免这种情况,当前的JIT只对经常执行的字节码进行编译,如循环等。

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

如下:

/**
     * Gets the default toolkit.
     *


     * If a system property named "java.awt.headless" is set
     * to true then the headless implementation
     * of Toolkit is used.
     *


     * If there is no "java.awt.headless" or it is set to
     * false and there is a system property named
     * "awt.toolkit",
     * that property is treated as the name of a class that is a subclass
     * of Toolkit;
     * otherwise the default platform-specific implementation of
     * Toolkit is used.
     *


     * 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;
    }


从上面我们知道了之所以要关闭JITjava.lang.Compiler.disable(); 是因为加快执行的速度。由于JIT对每条字节码都进行编译,造成了编译过程负担过重。为了避免这种情况,当前的JIT只对经常执行的字节码进行编译,如循环等。
阅读(19425) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~