protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { //同步操作,保证只能加载一次 //首先,在缓存中判断是否已经加载同名的类 Class<?> c = findLoadedClass(name); if (c == null) { longt0= System.nanoTime(); try { //parent:当前类加载器的父类加载器 if (parent != null) { //如果存在父类加载器,则调用父类加载器进行类的加载 c = parent.loadClass(name, false); } else { //parent为null:父类加载器是引导类加载器 c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader }
if (c == null) { //当前类加载器的父类加载器未加载此类 或 当前类加载器被加载 //调用当前ClassLoader longt1= 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; } }