• Home
  • Archives
  • 随笔
所有文章 友链 关于我

  • Home
  • Archives
  • 随笔

ClassLoader源码解析

发布于: 2018-10-23
更新于: 2023-07-09

类加载

  • 核心: loadClass(String name)
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);
                }
                //若父类为null,则使用BootstrapClassLoader
                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
                PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                PerfCounter.getFindClasses().increment();
            }
        }
        //若已加载的,则直接返回
        if (resolve) {
            resolveClass(c);
        }
        return c;
    }
}
  • 若loadClass()失败则启用findClass()而这也是用户所应该复写的方法
protected Class<?> findClass(String name) throws ClassNotFoundException {
        throw new ClassNotFoundException(name);
    }

双亲委派机制[Parents Delegation Model]

这里我觉得理解上时不能纠结于双亲,感觉这里应该是翻译不当。毕竟在加载时首先会委托给父辈s【表多个父辈】,eg:用户自定义->应用类->扩展类->启动类
而不是所谓的“双亲”委派,有时面试还会问为什么有双亲?😄

大部分使用双亲委派,还有很多不使用的场景JDK1.2前的findClass()、OSGI、JNDI的SPI接口

ClassLoader源码解析
/archives/e2c80eed/
作者
tyrantqiao
发布于
2018-10-23
更新于
2023-07-09
许可协议
CC BY-NC-SA 4.0
赏

蟹蟹大佬的打赏,大家一起进步

支付宝
微信
  • java
  • 面试
  • 源码

扫一扫,分享到微信

微信分享二维码
JDK11-String源码
leetcode题目详细解答【java版】持续更新
© 2024 tyrantqiao 本站总访问量次 本站访客数人次 载入天数...载入时分秒...
  • 所有文章
  • 友链
  • 关于我

tag:

  • 复盘
  • 我
  • 规划
  • java
  • 面试
  • 源码
  • 架构
  • Hadoop
  • HTTP
  • TCP
  • 学习笔记
  • IDEA
  • maven
  • idea
  • Java
  • jdk
  • 面经
  • linux
  • 爱情
  • mysql
  • 性能
  • sql
  • Mysql
  • JAVA
  • 技术
  • Redis
  • MQ
  • Spring
  • 数据库
  • TIDB
  • spring
  • unity
  • chatgpt
  • 经验分享
  • 前端
  • redis
  • vue
  • git
  • shadowsocks
  • hexo
  • blog
  • bug
  • 开发
  • 业务
  • jvm
  • 算法
  • MySQL
  • nginx
  • Linux
  • mq
  • db
  • springCloud
  • ssh
  • python
  • 爬虫
  • test
  • vim
  • 影视剧
  • 中间件
  • 事务
  • 性格
  • 音乐
  • 程序员
  • 随笔
  • mybatis
  • 演讲
  • 域名
  • 猫咪
  • 她
  • github
  • 计划
  • 旅游
  • 软件
  • 心理
  • 情商
  • 幽默
  • 才艺
  • 穿搭
  • 编程
  • 排序
  • 查找
  • 缓存
  • 网络
  • 设计模式
  • c
  • 课程设计
  • centos
  • 数学
  • 本网站主题yilia设计者的主页
如果有问题或者想讨论的可以联系[email protected]或者[email protected]