您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

jvm-类加载 自定义加载器和打破双亲委派

1:自定义类加载器,需要继承  ClassLoader  

双亲委派机制:第一个加载器先寻找是否加载过,找不到。先委托父类寻找该目标类,找不到,在委托上层父类加载器寻找目标,如果都找不到目标,则自己加载该类

自定义的加载器:默认的父加载器是应用程序加载器

打破双亲委派机制:自己创建的类,让第一个加载器直接加载,不委托父加载器寻找。 jdk等类是需要委托父加载器加载

代码:自定义类加载器实现打破双亲委派机制


import sun.misc.PerfCounter;

import java.io.FileInputStream;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: chenhao
 * \* Date: 2021/5/28
 * \* Time: 9:16
 * \* Description:
 * \
 */
public class MyLoader extends ClassLoader {
    private String classPath;

    public MyLoader(String classPath) {
         this.classPath = classPath;

    }

    private  ClassLoader parent=this.getParent();

    private byte[] loadByte(String name) throws Exception {
        name = name.replaceAll("\\.", "/");
        FileInputStream fis = new FileInputStream(classPath + "/" + name + ".class");
        int len = fis.available();
        byte[] data = new byte[len];
        fis.read(data);
        fis.close();
        return data;

    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            byte[] data = loadByte(name);
            return defineClass(name, data, 0, data.length);

        } catch (Exception e) {
            e.printStackTrace();
            throw new ClassNotFoundException();

        }
    }


    @Override
    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();

                    /**
                     * 打破双亲委派机制, 自己写的类,让走自己的loadClass加载,
                     * 不是自己的,还是走双亲委派
                     */
                    try {
                        if (this.getParent() != null  && !name.startsWith("com.example.design_pattern")) {
                            c = super.loadClass(name, false);
                        }
                    } 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;
            }

        }
    }

    public static void main(String args[]) throws Exception {
        //初始化自定义类加载器,会先初始化父类ClassLoader,其中会把自定义类加载器的父加载 器设置为应用程序类加载器
        // AppClassLoader
        MyLoader classLoader = new MyLoader("E:testjvm");

        Class clazz = classLoader.loadClass("com.example.design_pattern.jvm.User3");

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

        MyLoader classLoader1 = new MyLoader("E:testjvm2");

        Class clazz1 = classLoader1.loadClass("com.example.design_pattern.jvm.User3");


        System.out.println(clazz1.getClassLoader());
    }
}

 


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进