码迷,mamicode.com
首页 > 编程语言 > 详细

Java基础之ClassLoader类加载器简介

时间:2018-12-13 01:24:19      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:may   ret   fir   creat   count   类加载   object   defining   syn   

classloader简介

1 classloader层次结构(父子关系)

Bootstrap(ClassLoader)

  • ExtClassLoader
    • AppClassLoader
      • XXXClassLoader

详见代码如下:

sun.misc.Launcher
public Launcher() {

        Launcher.ExtClassLoader var1;

        try {

            var1 = Launcher.ExtClassLoader.getExtClassLoader();

        } catch (IOException var10) {

            throw new InternalError("Could not create extension class loader", var10);

        }

 

        try {

            this.loader = Launcher.AppClassLoader.getAppClassLoader(var1);

        } catch (IOException var9) {

            throw new InternalError("Could not create application class loader", var9);

        }

2 classloader加载类过程

classloader在加载class时优先尝试通过parent classloader加载,如果加载不到,才尝试自己加载,即常说的“双亲委派”,

并且如果一个class依赖其他class,比如ClassA依赖ClassB,ClassB默认由ClassA的classloader来加载。

* <p> The methods and constructors of objects created by a class loader may
* reference other classes. To determine the class(es) referred to, the Java
* virtual machine invokes the {@link #loadClass <tt>loadClass</tt>} method of
* the class loader that originally created the class.

详见代码如下:

java.lang.ClassLoader
protected Class<?> loadClass(String name, boolean resolve)

        throws ClassNotFoundException

    {

        synchronized (getClassLoadingLock(name)) {

            // First, check if the class has already been loaded

            Class<?> c = findLoadedClass(name);

            if (c == null) {

                long t0 = System.nanoTime();

                try {

                    if (parent != null) {

                        c = parent.loadClass(name, false);

                    } else {

                        c = findBootstrapClassOrNull(name);

                    }

                } catch (ClassNotFoundException e) {

                    // ClassNotFoundException thrown if class not found

                    // from the non-null parent class loader

                }

 

                if (c == null) {

                    // If still not found, then invoke findClass in order

                    // to find the class.

                    long t1 = System.nanoTime();

                    c = findClass(name);

 

                    // this is the defining class loader; record the stats

                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);

                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);

                    sun.misc.PerfCounter.getFindClasses().increment();

                }

            }

            if (resolve) {

                resolveClass(c);

            }

            return c;

        }

    }

 

Java基础之ClassLoader类加载器简介

标签:may   ret   fir   creat   count   类加载   object   defining   syn   

原文地址:https://www.cnblogs.com/barneywill/p/10111633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!