反射常见实例改进Class.forName耗时问题

这篇博客探讨了Java中反射创建实例的性能,通过对比`Class.forName`和`newInstance`的方法耗时,发现`Class.forName`的开销较大。文章进一步提出了缓存`Class`对象的优化策略,显著减少了运行时间。实验结果显示,使用缓存后的反射操作性能得到了显著提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其他类

public abstract class ProductAbstract {
    public void test() {
        System.out.println("--------------test----");
    }
}

public class ProductA  extends ProductAbstract{
}


public class ProductB  extends ProductAbstract{
}

测试反射创建实例耗时

package com.xy.designmode.factory;

import com.xy.util.TimeUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class TestFactory {
    private static Map<String, String> map = new HashMap<>();
    private static Map<String, Class<?>> classCache = new HashMap<>();

    static {
        map.put("productA", "com.xy.designmode.factory.ProductA");
        map.put("productB", "com.xy.designmode.factory.Productb");
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        TimeUtil.time("ALL");
        testClassForNameUseTime();
        TimeUtil.time("ALL");
    }

    private static void testClassForNameUseTime() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        TimeUtil.time();
        String objKey = "productA";
        String clazz = map.get(objKey);
        Class<?> aClass = null;
        for (int i = 0; i < 100000; i++) {
            aClass = Class.forName(clazz);

        }
        TimeUtil.time();
        for (int i = 0; i < 100000; i++) {
            aClass.getConstructor().newInstance();
        }
        TimeUtil.time();
    }
}

testClassForNameUseTime()

 间隔:235毫秒   --- Class.forName
 间隔:26毫秒     --newInstance

结论:  Class.forName耗时远大于 newInstance

改进: class放入缓存


import com.xy.util.TimeUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class TestFactory {
    private static Map<String, String> map = new HashMap<>();
    private static Map<String, Class<?>> classCache = new HashMap<>();

    static {
        map.put("productA", "com.xy.designmode.factory.ProductA");
        map.put("productB", "com.xy.designmode.factory.Productb");
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        TimeUtil.time("ALL");
        testClassForNameUseTime2();
        TimeUtil.time("ALL");
    }    
private static void testClassForNameUseTime2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        TimeUtil.time();
        String objKey = "productA";
        String clazz = map.get(objKey);
        Class<?> aClass = null;
        for (int i = 0; i < 100000; i++) {
              aClass = classCache.get(objKey);
            if(aClass == null){
                aClass = Class.forName(clazz);
                classCache.put(objKey,aClass);
            }
        }
        TimeUtil.time();
        for (int i = 0; i < 100000; i++) {
            aClass.getConstructor().newInstance();
        }
        TimeUtil.time();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值