原文链接:为什么阿里不建议用excutors创建线程池 – 编程屋
1 前言:
大家都知道,阿里规范中有一条是不允许用excutors去创建线程池,而是采用ThreadPoolExecutor的原生方式去创建。很早就听过所过这种说法,但是一直都没去搞清楚是为什么,今天就查阅资料去了解了这个问题。
2 Excutors创建线程的方式
通过Excutors来创建线程池,有4种创建线程的方法。
- newCachedThreadPool创建一个可缓存线程池,如果线程池的大小超过了处理任务所需的线程,那么就会回收部份空闲线(60秒不处理任务)线程,当任务数增加时,此线程有可以智能的添加新线程来处理任务。此线程池不会对线程池的大小做限制,线程池的大小完全依赖于操作系统能够创建的最大线程池大小。
- newFixedThreadPool 创建固定大小线程池,提交一个任务就创建一个线程。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。
- newSingleThreadExecutor 创建一个单线程化的线程池,这个线程池只有一个线程池在工作。如果这个唯一的线程因为异常结束,那么就会有一个新的线程来替代它,此线程池保证所有任务的执行顺序按照任务的提交顺序来执行。
- newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。(自己可以点进源码中看,本质也是调用了ThreadPoolExecutor()方法来实现的
大家看以上4个创建线程池的方式,可以发现其实最终都是调用了ThreadPoolExecutor()方法来实现的。(这里不展开,之后会特意讲到)
一般不采用excutors直接创建线程池可以防止OOM,同时也可以更好的理解线程池的构建原理。
我们来用excutors直接创建线程池模仿一个产生OOM异常的场景。
代码块:我们用newFixedThreadPool创建一个固定大小的线程池。让其一直去执行任务,并且为了更好的模拟OOM,我们设置VM options
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
while (true) {
executorService.execute(new Task());
}
}
static class Task implements Runnable{
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果:
我们发现GC了。
这是我们可以用jps找到运行类的pid并且执行jstack 13548>13548.log。
13548.log:
2021-11-08 11:01:48
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.112-b15 mixed mode):
"DestroyJavaVM" #19 prio=5 os_prio=0 tid=0x00000000034d4000 nid=0x2084 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"pool-1-thread-5" #18 prio=5 os_prio=0 tid=0x0000000016b5b000 nid=0x21e8 waiting on condition [0x00000000177af000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.liubujun.thread.TestExcutor$Task.run(TestExcutor.java:27)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
"pool-1-thread-4" #17 prio=5 os_prio=0 tid=0x0000000016b58800 nid=0x20b8 waiting on condition [0x00000000176ae000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.liubujun.thread.TestExcutor$Task.run(TestExcutor.java:27)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
"pool-1-thread-3" #16 prio=5 os_prio=0 tid=0x0000000016b55800 nid=0x2618 waiting on condition [0x00000000175ae000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.liubujun.thread.TestExcutor$Task.run(TestExcutor.java:27)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
"pool-1-thread-2" #15 prio=5 os_prio=0 tid=0x0000000015e47000 nid=0x4bd0 waiting on condition [0x00000000174ae000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.liubujun.thread.TestExcutor$Task.run(TestExcutor.java:27)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
"pool-1-thread-1" #14 prio=5 os_prio=0 tid=0x0000000015e46800 nid=0xccc waiting on condition [0x00000000173ae000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.liubujun.thread.TestExcutor$Task.run(TestExcutor.java:27)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
"Service Thread" #13 daemon prio=9 os_prio=0 tid=0x0000000015dd0000 nid=0x3b04 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #12 daemon prio=9 os_prio=2 tid=0x0000000015d17000 nid=0x854 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #11 daemon prio=9 os_prio=2 tid=0x0000000015d0f800 nid=0x48b4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #10 daemon prio=9 os_prio=2 tid=0x0000000015d0c800 nid=0x5504 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #9 daemon prio=9 os_prio=2 tid=0x0000000015d0c000 nid=0x41c8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Command Reader" #8 daemon prio=10 os_prio=0 tid=0x0000000015af8800 nid=0x5090 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Event Helper Thread" #7 daemon prio=10 os_prio=0 tid=0x0000000015af5800 nid=0x5294 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Transport Listener: dt_socket" #6 daemon prio=10 os_prio=0 tid=0x0000000015ae9000 nid=0x610c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000015ae1000 nid=0x4df0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000015a8a000 nid=0xc9c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000015a71000 nid=0x6edc in Object.wait() [0x0000000015f4e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ffcc8e60> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x00000000ffcc8e60> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000013b7d000 nid=0x10f0 in Object.wait() [0x0000000015a4e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ffca5840> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000ffca5840> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x0000000013b79000 nid=0x5724 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000034ea000 nid=0x91c runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000034eb800 nid=0x6130 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000034ed000 nid=0x3bb0 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000034ee800 nid=0x4dd0 runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000000034f0800 nid=0x51f4 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000000034f2000 nid=0x10e4 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000000034f6000 nid=0x540 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000000034f7000 nid=0x20b4 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x0000000015e17000 nid=0x621c waiting on condition
JNI global references: 1795
但我们明明已经设置了固定线程数量为5。已经有线程去执行任务,为什么还会发生OOM呢。我们根据报错信息最后发现是ThreadPoolExecutor.execute的1361行出错,我们追踪来看
发现报错这一行是一个if的判断,前面是一个线程池的状态判断,不会出现OOM,原因就出在workQueue.offer(command)),我们可以追踪一下这个方法发现其有多个实现,而我们用newFixedThreadPool的阻塞队列用的是LinkedBlockingQueue<Runnable>()
我们追踪到最后发现是416行,new了一个节点,然后将我们的任务放了进去。因为我的代码了任务是sleep了10秒钟,所以线程任务的执行速度远远小于线程队列的创建速度,我一直都在向任务对列中放任务,它就会一直new Node知道空间内存不够发生OOM.
所以当我们的代码耗时较长时并且又采用了excutors去创建线程池,就有可能发生OOM风险。因此一般采用ThreadPoolExecutor()来创建线程池,设置合理的参数和拒绝策略。
以上只是部分内容,为了维护方便,本文已迁移到新地址:为什么阿里不建议用excutors创建线程池 – 编程屋