报错 Caused by: java.lang.IllegalArgumentException: null

 代码在X86的架构上运行没有问题,放到aarch64上代码报错了

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pullGroupMessageTask' defined in URL [jar:file:/home/changfa/boot/im/im-server.jar!/BOOT-INF/classes!/com/bx/imserver/task/PullGroupMessageTask.class]: Bean instantiation via constructor failed; nested exception is java.lang.ExceptionInInitializerError
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:291) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:921) ~[spring-context-5.3.30.jar!/:5.3.30]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.30.jar!/:5.3.30]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409) [spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) [spring-boot-2.7.17.jar!/:2.7.17]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289) [spring-boot-2.7.17.jar!/:2.7.17]
        at com.bx.imserver.IMServerApp.main(IMServerApp.java:16) [classes!/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_412]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_412]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_412]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_412]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [im-server.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [im-server.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [im-server.jar:na]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [im-server.jar:na]
Caused by: java.lang.ExceptionInInitializerError: null
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_412]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_412]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_412]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_412]
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.3.30.jar!/:5.3.30]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:306) ~[spring-beans-5.3.30.jar!/:5.3.30]
        ... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: null
        at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1314) ~[na:1.8.0_412]
        at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1272) ~[na:1.8.0_412]
        at com.bx.imcommon.util.ThreadPoolExecutorFactory.getThreadPoolExecutor(ThreadPoolExecutorFactory.java:65) ~[im-commom-2.0.0.jar!/:na]
        at com.bx.imserver.task.AbstractPullMessageTask.<clinit>(AbstractPullMessageTask.java:16) ~[classes!/:na]
        ... 33 common frames o

报错的类

package com.bx.imcommon.util;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 创建单例线程池
 * @author Andrews
 * @date 2023/11/30 11:12
 */
@Slf4j
public final class ThreadPoolExecutorFactory {
    /**
     * 机器的CPU核数:Runtime.getRuntime().availableProcessors()
     * corePoolSize 池中所保存的线程数,包括空闲线程。
     * CPU 密集型:核心线程数 = CPU核数 + 1
     * IO 密集型:核心线程数 = CPU核数 * 2
     */
    private static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
    /**
     * maximumPoolSize - 池中允许的最大线程数(采用LinkedBlockingQueue时没有作用)。
     */
    private static final int MAX_IMUM_POOL_SIZE = 100;
    /**
     * keepAliveTime -当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间,线程池维护线程所允许的空闲时间
     */
    private static final int KEEP_ALIVE_TIME = 1000;
    /**
     * 等待队列的大小。默认是无界的,性能损耗的关键
     */
    private static final int QUEUE_SIZE = 200;

    /**
     * 线程池对象
     */
    private static volatile ThreadPoolExecutor threadPoolExecutor = null;

    /**
     * 构造方法私有化
     */
    private ThreadPoolExecutorFactory() {
        if (null == threadPoolExecutor) {
            threadPoolExecutor = ThreadPoolExecutorFactory.getThreadPoolExecutor();
        }
    }

    /**
     * 重写readResolve方法
     */
    private Object readResolve() {
        //重写readResolve方法,防止序列化破坏单例
        return ThreadPoolExecutorFactory.getThreadPoolExecutor();
    }

    /**
     * 双检锁创建线程安全的单例
     */
    public static ThreadPoolExecutor getThreadPoolExecutor() {
        if (null == threadPoolExecutor) {
            synchronized (ThreadPoolExecutorFactory.class) {
                if (null == threadPoolExecutor) {
                    threadPoolExecutor = new ThreadPoolExecutor(
                            //核心线程数
                            CORE_POOL_SIZE,
                            //最大线程数,包含临时线程
                            MAX_IMUM_POOL_SIZE,
                            //临时线程的存活时间
                            KEEP_ALIVE_TIME,
                            //时间单位(毫秒)
                            TimeUnit.MILLISECONDS,
                            //等待队列
                            new LinkedBlockingQueue<>(QUEUE_SIZE),
                            //拒绝策略
                            new ThreadPoolExecutor.CallerRunsPolicy()
                    );
                }
            }
        }
        return threadPoolExecutor;
    }

    /**
     * 关闭线程池
     */
    public void shutDown() {
        if (threadPoolExecutor != null) {
            threadPoolExecutor.shutdown();
        }
    }

    public void execute(Runnable runnable) {
        if (runnable == null) {
            return;
        }
        threadPoolExecutor.execute(runnable);
    }

}

解决方式 

调整 CORE_POOL_SIZEMAX_IMUM_POOL_SIZE。对于 IO 密集型任务,CORE_POOL_SIZE 通常设为 CPU 核心数的 2 倍,而最大线程数应至少与核心线程数相同,以确保不会引起配置错误。

调整后的代码如下:

package com.bx.imcommon.util;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 创建单例线程池
 * @author Andrews
 * @date 2023/11/30 11:12
 */
@Slf4j
public final class ThreadPoolExecutorFactory {
    /**
     * 机器的CPU核数:Runtime.getRuntime().availableProcessors()
     * corePoolSize 池中所保存的线程数,包括空闲线程。
     * CPU 密集型:核心线程数 = CPU核数 + 1
     * IO 密集型:核心线程数 = CPU核数 * 2
     */
    private static final int CPU_CORES = Runtime.getRuntime().availableProcessors();

    private static final int CORE_POOL_SIZE = CPU_CORES * 2; // 核心线程数为 CPU 核心数的 2 倍

    /**
     * maximumPoolSize - 池中允许的最大线程数(采用LinkedBlockingQueue时没有作用)。
     */
    private static final int MAX_IMUM_POOL_SIZE = CORE_POOL_SIZE; // 确保最大线程数至少为核心线程数
    /**
     * keepAliveTime -当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间,线程池维护线程所允许的空闲时间
     */
    private static final int KEEP_ALIVE_TIME = 1000;
    /**
     * 等待队列的大小。默认是无界的,性能损耗的关键
     */
    private static final int QUEUE_SIZE = 200;

    /**
     * 线程池对象
     */
    private static volatile ThreadPoolExecutor threadPoolExecutor = null;

    /**
     * 构造方法私有化
     */
    private ThreadPoolExecutorFactory() {
        if (null == threadPoolExecutor) {
            threadPoolExecutor = ThreadPoolExecutorFactory.getThreadPoolExecutor();
        }
    }

    /**
     * 重写readResolve方法
     */
    private Object readResolve() {
        //重写readResolve方法,防止序列化破坏单例
        return ThreadPoolExecutorFactory.getThreadPoolExecutor();
    }

    /**
     * 双检锁创建线程安全的单例
     */
    public static ThreadPoolExecutor getThreadPoolExecutor() {
        if (null == threadPoolExecutor) {
            synchronized (ThreadPoolExecutorFactory.class) {
                if (null == threadPoolExecutor) {
                    log.info("初始化 ThreadPoolExecutor,核心线程数: {}, 最大线程数: {}, 存活时间: {}, 队列大小: {}",
                            CORE_POOL_SIZE, MAX_IMUM_POOL_SIZE, KEEP_ALIVE_TIME, QUEUE_SIZE);
                    threadPoolExecutor = new ThreadPoolExecutor(
                            //核心线程数
                            CORE_POOL_SIZE,
                            //最大线程数,包含临时线程
                            MAX_IMUM_POOL_SIZE,
                            //临时线程的存活时间
                            KEEP_ALIVE_TIME,
                            //时间单位(毫秒)
                            TimeUnit.MILLISECONDS,
                            //等待队列
                            new LinkedBlockingQueue<>(QUEUE_SIZE),
                            //拒绝策略
                            new ThreadPoolExecutor.CallerRunsPolicy()
                    );
                }
            }
        }
        return threadPoolExecutor;
    }

    /**
     * 关闭线程池
     */
    public void shutDown() {
        if (threadPoolExecutor != null) {
            threadPoolExecutor.shutdown();
        }
    }

    public void execute(Runnable runnable) {
        if (runnable == null) {
            return;
        }
        threadPoolExecutor.execute(runnable);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值