Java 面试 多线程遇到的问题,如何处理

问题描述

某服务在运行过程中抛出了 RejectedExecutionException。

配置参数: corepoolsize = 50, maxpoolsize =50, workqueue 为 SynchronousQueue

现象: 当新的task被拒绝时,pool size未达到配置值50.

复制代码
Caused by: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@3c359b91 rejected from java.util.concurrent.ThreadPoolExecutor@755319d6[Running, pool size = 42, active threads = 27, queued tasks = 0, completed tasks = 29]
	at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
	at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
	at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)

分析

日志中的打印是 ThreadPoolExecutor的 toString()方法的输出

  • Running 对应 ctl 的状态位,
  • pool size对应 workers.size(),
  • active threads对应处于locked的线程数(执行状态)
  • queued tasks对应 workQueue.size()
  • completed tasks对应完成的task数
  • RejectedExecutionException 说明在 addWorker()函数中获取到的 ctl中的worker数量已经达到了maxpoolsize 50.

疑问

1、ctl中记录的 worker 数量和 worker.size () 在什么情况下会不一致?

这个主要由于toString()⽅法输出的是works的⼤⼩,⽽加任务时是判断的ctl中的线程数⽬,ctl中的线程数⽬并不⼀定等于真实的线程数⽬,如下为代码中的注释:

The workerCount is the number of workers that have been permitted to start and not permitted to stop. The value may be transiently different from the actual number of live threads, for example when a ThreadFactory fails to create a thread when asked, and when exiting threads are still performing bookkeeping before terminating. The user-visible pool size is reported as the current size of the workers set. 即ctl中的workerCount是被允许执⾏且不允许停⽌的线程数⽬,这个值可能会短暂性的与真实活跃的线程数量不同。

2、pool size和 active threads 为什么不一致?

分析出以上结论后,我们只需要创建多个线程往线程池丢任务即可,然后让⼤多数线程将ctl中的workCount加1,并让线程停到将worker加⼊到workers之前停⽌,这样⽤户看到的⽇志就会出现,虽然workers的⼤⼩很⼩,就出现了拒绝加⼊的现象。 demo:

java 复制代码
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Test {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1,
                TimeUnit.MINUTES, new ArrayBlockingQueue<>(2));
        for (int i = 0; i < 30; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    threadPoolExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("I was executed!");
                        }
                    });
                }
            }).start();
        }
    }
}

这⾥将线程0-1置为active状态,将2-9停到增加完ctl值后,即break retry; 语句,然后10-11会加⼊到队列中,当加⼊线程12启动时,就会出现拒绝加⼊的异常,如下图:

如果想让队列中元素为0,则只需让被拒绝线程在打印错误信息之前,让活跃线程将队列中的任务执⾏完即可,如下为结果中的⽇志:

同时,由于worker在获取执⾏任务前,会短暂的释放掉锁(⽤来判断worker是否为活跃状态的锁),所以也可以让active thread⼩于pool thread,如下:

⾄此,⽇志中的所有现象都可以被解释了。故为正常现象

相关推荐
怕浪猫9 小时前
外包和大厂的前端代码,差在凌晨三点
前端·面试·github
十年Java程序媛15 小时前
Java this 与 super 详解|继承高频踩坑 + 面试题整理
java·spring boot·面试
萧瑟余晖15 小时前
Spring Cloud配置中心与动态刷新详解
spring cloud·面试
白露与泡影17 小时前
一线 Java 后端面试八股(2026 更新版,含完整答案)
java·开发语言·面试
学到老活到老1 天前
集合换成线程安全的,就不用加锁了吗?
java·面试
萧瑟余晖1 天前
Spring Cloud Alibaba生态整合详解
spring·spring cloud·面试
Sailing1 天前
【封闭结束2】Agent 意图识别:从语义理解到稳定路由
人工智能·面试·agent
Sailing1 天前
【封闭结束1】端到端交付:Agent 一次请求如何走完全链路
人工智能·面试·agent
怕浪猫1 天前
红利窗口开着,程序员别在场边囤课
面试·github·ai编程
lll23526917792 天前
HR如何快速整理面试沟通内容?
面试·职场和发展