java里面泛型会将基本类型转为包装类

1 篇文章 0 订阅
1 篇文章 0 订阅

昨天我利用反射写一个通用的接口性能测试,期间遇到了一个奇怪的问题,先贴代码。
单元测试类:OilPerformanceTest

public class OilPerformanceTest {
    Logger logger = Logger.getLogger(OilPerformanceTest.class);
    static long startTime = System.currentTimeMillis();
    static int testMinute = 5;
    public static AtomicInteger time = new AtomicInteger(0);
    static DataCloudClient datacloudClient = new DataCloudClient();
    static String[] cityName = new String[] { "北京", "吉林", "上海", "江苏", "天津", "江西", "重庆", "辽宁", "安徽", "内蒙古", "福建", "宁夏", "甘肃", "青海", "广东", "山东", "广西",
            "山西", "贵州", "陕西", "海南", "四川", "河北", "西藏", "河南", "新疆", "黑龙江", "云南", "湖北", "浙江", "湖南" };

    @Test
    public void testPM25Performance() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        String[] textStrings = new String[] {};
        System.out.println(textStrings.getClass());
        System.out.println(String.class);
        try {
            PropertyConfigurator.configure(PathUtil.getWholePath("etc/log4j.properties"));
            PerformanceThreadPool.run(OilPerformanceThread.class, startTime, testMinute, time, datacloudClient, cityName);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}

性能测试线程池:PerformanceThreadPool

package com.unisound.dcs.client.performanceTest.oil;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

public class PerformanceThreadPool {
    private static Logger logger = Logger.getLogger(PerformanceThreadPool.class);

    private PerformanceThreadPool() {
        super();
    }

    public static <T> void run(Class<? extends Runnable> rClass, T... parmas) throws NoSuchMethodException, SecurityException,
            InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        ExecutorService executorService = Executors.newFixedThreadPool(60);
        List<Class<?>> classesList = new ArrayList<Class<?>>();
        for (T parma : parmas) {
            classesList.add(parma.getClass());
            System.out.println(parma.getClass().getName());
        }
        Constructor<? extends Runnable> constructor = rClass.getConstructor(classesList.toArray(new Class<?>[] {}));
        for (int i = 0; i < 60; i++) {
            executorService.execute(constructor.newInstance(parmas));
        }
        executorService.shutdown();
        // 等待子线程结束,再继续执行下面的代码
        try {
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("all thread complete!");
        // logger.info("总次数:" + PerformanceThreadPool.time.get());
    }
}

线程类:OilPerformanceThread

package com.unisound.dcs.client.performanceTest.oil;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;

import com.unisound.dcs.client.DataCloudClient;

public class OilPerformanceThread implements Runnable {
    Logger logger = Logger.getLogger(OilPerformanceThread.class);
    long startTime;
    int testMinute;
    AtomicInteger time;
    DataCloudClient datacloudClient;
    String[] cityName;

    public OilPerformanceThread(long startTime, int testMinute, AtomicInteger time, DataCloudClient datacloudClient, String[] cityName) {
        super();
        this.startTime = startTime;
        this.testMinute = testMinute;
        this.time = time;
        this.datacloudClient = datacloudClient;
        this.cityName = cityName;
    }

    public void run() {
        while ((System.currentTimeMillis() - startTime) <= 60 * 1000 * testMinute) {
            int index = (int) (Math.random() * cityName.length);
            logger.info(datacloudClient.getOilInfo(cityName[index], ""));
            time.incrementAndGet();
        }
    }

}

利用反射我动态的创建线程类实例,但是却报出了:java.lang.NoSuchMethodException:com.unisound.dcs.client.performanceTest.oil.OilPerformanceThread.<init>(java.lang.Long, java.lang.Integer, java.util.concurrent.atomic.AtomicInteger, com.unisound.dcs.client.DataCloudClient, [Ljava.lang.String;)
找不到线程类的构造器,仔细观察,线程类成员变量:
long startTime;
int testMinute;

他们是基本类型,看上面的报错我要得到的构造器为:com.unisound.dcs.client.performanceTest.oil.OilPerformanceThread.<init>(java.lang.Long, java.lang.Integer, java.util.concurrent.atomic.AtomicInteger, com.unisound.dcs.client.DataCloudClient, [Ljava.lang.String;)
startTime,testMinute变成了包装类型!再看我获取参数类型的那段代码,他在线程池类里面:

for (T parma : parmas) {
            classesList.add(parma.getClass());
            System.out.println(parma.getClass().getName());
        }

虽然我传进来的是基本类型的int和long但是泛型会将基本类型变成了包装类型,从而得到的不是我期望的int.class 和long.class,二是得到是Integer.class 和Long.class,我的线程类的构造器是:

public OilPerformanceThread(long startTime, int testMinute, AtomicInteger time, DataCloudClient datacloudClient, String[] cityName) {
        super();
        this.startTime = startTime;
        this.testMinute = testMinute;
        this.time = time;
        this.datacloudClient = datacloudClient;
        this.cityName = cityName;
    }

显然找不到该构造器!将线程类的基本类型改成包装类型即可解决此问题。

得到结论:java泛型变长参数(T… param)会将传进方法的基本类型转为包装类

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值