【上线复盘】上线时线程池使用问题

线程池使用问题

问题是个小问题,但暴露的其它问题比较多,记录一下。
原因是需要使用线程池做异步处理,实现是使用java.util.concurrent.ExecutorService,在配置线程池的基本大小corePoolSize参数时,通过使用Runtime.getRuntime().availableProcessors()获取Java虚拟机的可用的处理器数量,来初始化线程池基本大小,设置数量为处理器数量*2

	/**
     * 创建线程池
     * @return
     */
    @Bean
    public ExecutorService createThreadPool() {
        ExecutorService executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2,
                20, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024));
        return executorService;
    }

程序修改上线前经过了本地环境、开发环境、测试环境、预发环境的检验,结果线上环境出了问题,启动时报异常:

Caused by: java.lang.IllegalArgumentException: null
	at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1307)
	at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1195)
	at com.xxx.xxx.xxx.config.ExcutorServiceConfig.createThreadPool(ExcutorServiceConfig.java:27)

看日志错误是ExecutorService类报的IllegalArgumentException错误,再查看ExecutorService源码:

/**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
		if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        ...
        ...

方法头注释里对于IllegalArgumentException的说明为:
corePoolSize小于0
或者maximumPoolSize小于等于0
或者maximumPoolSize小于corePoolSize
或者keepAliveTime小于0
会抛出IllegalArgumentException异常

当时看到 null 时,被带向了是空值的方面,其实后来发现是maximumPoolSize小于corePoolSize的问题,因为线上node1节点的服务器CPU核数为12,乘以2后大于20,而其它环境CPU核数都小于10。

问题1:
对ExecutorService类不是太熟,只知道使用线程池要用这个类,但还是比较粗浅的了解

问题2:
研发和运维分开的团队,在涉及性能优化的时候,一定要走设计评审,特别是涉及CPU、内存

问题3:
错误提示要明确,比如ExecutorService类中的异常信息抛出的时候,不能很容易的看出问题原因,特别是上线的功能比较多,马上找不到问题的原因,会影响上线的时间

问题4:
其实在使用Spring框架的时候,线程池有更好的解决方案ThreadPoolExecutor

问题5:
在使用线程池时,没有考虑如何正确地关闭线程池,当服务重启或升级,没有对进行中的线程有好的处理,万一数据有影响,再进行数据修正会特别麻烦

解决方案:

  1. 使用ThreadPoolExecutor来实现,初始化参数与运维一起讨论更合适的数量,并且针对线程池的关闭要做好处理
  2. 规范开发流程,开发阶段有超出设计时的功能或优化时,进行补充设计评审,如果是技术上的优化,组织其他组长和上级领导参加评审
  3. 复盘并对线程池进行专门技术分享,提高开发人员的技术水平
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
工作复盘: 在过去的一周中,我们团队面临了一些挑战,但也取得了一些进展。以下是我对关键问题和工作的复盘: 1. 系统上线问题解答: 我们在上线新系统遇到了一些问题,但我们团队迅速响应,积极解决了这些问题。通过及的沟通和协调,我们能够迅速识别并解决了与系统上线相关的各种技术和功能问题。这种团队合作和高效沟通的表现非常令人鼓舞。 2. SVN权限整理: 为了更好地管理代码库,我们进行了SVN权限整理工作。我们检查了每个团队成员的权限,并根据他们的角色和职责进行了适当的调整。这有助于确保每个人都能够访问他们所需要的代码,并保护代码库的安全性。这项工作虽然耗费了一些间和精力,但它对于团队的整体效率和协作能力非常重要。 3. 工作沟通支持: 在过去的一周中,我特别关注了团队中的工作沟通。我尽量保持开放和透明,鼓励团队成员之间的互动和交流。我提供了必要的支持和帮助,解答了他们在工作中遇到的问题。通过及的反馈和有效的沟通,我们能够更好地理解彼此的需求,并更好地协作完成工作。 总结起来,我们在解决系统上线问题、进行SVN权限整理以及工作沟通方面取得了一些重要的进展。这些经验和教训将帮助我们改进工作流程,加强团队合作,并提高工作效率。我们应该继续保持积极的态度,持续学习和改进,以应对未来的挑战。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值