java基础学习笔记之类加载器

网友投稿 183 2023-07-22


java基础学习笔记之类加载器

类加载器

java类加载器就是在运行时在JVM中动态地加载所需的类,java类加载器基于三个机制:委托,可见,单一。

把classpath下的那些.class文件加载进内存,处理后成为字节码,这些工作是类加载器做的。

委托机制指的是将加载类的请求传递给父加载器,如果父加载器找不到或者不能加载这个类,那么再加载他。

可见性机制指的是父加载器加载的类都能被子加载器看见,但是子加载器加载的类父加载器是看不见的。

单一性机制指的是一个类只能被同一种加载器加载一次。

默认类加载器

系统默认三个类加载器:

BootStrap

ExtClassLoader

AppClassLoader

类加载器也是java类,而BootStrap不是。 验证代码:

public class ClassLoaderTest {

public static void main(String[] args) {

System.out.println(System.class.getClassLoader());

}

}

输出:null

如果使用System.out.println(System.class.getClassLoader().toString);,则报空指针异常:

Exception in thread "main" java.lang.NullPointerException

at com.iot.classloader.ClassLoaderTest.main(ClassLoaderTest.java:10)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:483)

http:// at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

可见,System类是由BootStrap类加载器加载。

类加载器的委托机制

类加载器的树状图

类加载器

一般加载类的顺序:

首先当前线程的类加载器去加载线程中的第一个类

如果类A应用了类B,java虚拟机将使用加载类A的类加载器来加载类B

还可以直接调用ClassLoader.loadClass()方法来制定某个类加载器去加载某个类

自定义类加载器的编写原理

API:

Class ClassLoader

模板方法设计模式

父类:

loadClass(类加载的流程,模板)

findClass供子类覆盖的、被loadClass方法调用的类加载逻辑

defineClass得到class文件转换成字节码

子类:覆盖findClass方法

例子:

loadClass方法的源码

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;

}

}

API文档中的例子:

class NetworkClassLoader extends ClassLoader {

String host;

int port;

public Class findClass(String name) {

byte[] b = loadClassData(name);

return defineClass(name, b, 0, b.length);

}

private byte[] loadClassData(String name) {

// load the class data from the connection

. . .

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java8时间日期库中的常用使用示例
下一篇:汇总java调用python方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~