3.1.1 类加载机制

运行时的数据区

在这里插入图片描述

类生命周期

在这里插入图片描述

类加载器

在这里插入图片描述

验证问题

  • 查看类对应的加载器
  • JVM如何知道我们的类在何方
  • 类不会重复加载
  • 类的卸载
  • 双亲委派模型

查看类对应的加载器

在这里插入图片描述

public class ClassLoaderView {
    public static void main(String[] args) throws ClassNotFoundException {
        System.out.println("核心类库加载器:" +
                ClassLoaderView.class.getClassLoader().loadClass("java.lang.String").getClassLoader());
        System.out.println("扩展类库加载器:" +
                ClassLoaderView.class.getClassLoader().loadClass("com.sun.nio.zipfs.ZipCoder").getClassLoader());
        System.out.println("应用程序库加载器:" + ClassLoaderView.class.getClassLoader());

        //双亲委派模型
        System.out.println("应用程序库加载器的父类:" + ClassLoaderView.class.getClassLoader().getParent());
        System.out.println("应用程序库加载器的父类的父类:" + ClassLoaderView.class.getClassLoader().getParent().getParent());

    }
}

JVM如何知道我们的类在何方

在这里插入图片描述

类不会重复加载

类的唯一性:同一个类加载器,类名一样,代表是同一个类。

识别方式:ClassLoader Instance id + PackageName + ClassName

验证方式:使用类加载器,对同一个class类的不同版本,进行多次加载,检查是否会加载到最新的代码。

public class LoaderTest {
    public static void main(String[] args) throws Exception {
        URL classUrl = new URL("file:D:\\");//jvm 类放在位置

        URLClassLoader parentLoader = new URLClassLoader(new URL[]{classUrl});

        while (true) {
            // 创建一个新的类加载器
            URLClassLoader loader = new URLClassLoader(new URL[]{classUrl});

            // 问题:静态块触发
            Class clazz = loader.loadClass("HelloService");
            System.out.println("HelloService所使用的类加载器:" + clazz.getClassLoader());

            Object newInstance = clazz.newInstance();
            Object value = clazz.getMethod("test").invoke(newInstance);
            System.out.println("调用getValue获得的返回值为:" + value);

            Thread.sleep(3000L); // 1秒执行一次
            System.out.println();

            //  help gc  -verbose:class
            newInstance = null;
            loader = null;

        }

        // System.gc();
    }
}
结论:
在同一个类加载器上,同一个类不会重复加载。
静态块的代码在类加载时不会触发而是在对象第一次实例化的时候被触发。

类的卸载

在这里插入图片描述

双亲委派模型

在这里插入图片描述
热加载:一个类在同一个类加载器中不会重复加载,那么为了得到多次加载一个类的效果,在每次加载的类时候创建一个新的类加载器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值