当前位置:网站首页>使用无界队列的线程池会导致内存飙升吗?
使用无界队列的线程池会导致内存飙升吗?
2022-08-02 11:11:00 【华为云】
Executors创建线程池方式有如下几种:
Executors.newFixedThreadPool(10);//LinkedBlockingQueue 无限加入队列Executors.newScheduledThreadPool(10);//DelayedWorkQueue 队列如果满了,阻塞Executors.newSingleThreadScheduledExecutor();//DelayedWorkQueue 队列如果满了,阻塞Executors.newCachedThreadPool();//SynchronousQueue 队列如果满了,抛异常Executors.newSingleThreadExecutor();//LinkedBlockingQueue 无限加入队列
具体细节说明文章:Java线程池Executor详解
本文以Executors.newFixedThreadPool为例
定长线程池,核心线程数和最大线程数由用户传入,超出在队列等待,以下为相关源码。
//newFixedThreadPool创建线程池源码public static ExecutorService newFixedThreadPool(int nThreads) { /** * corePoolSize : 核心线程的数量为自定义输入nThreads * maximumPoolSize : 最大线程的数量为自定义输入nThreads * keepAliveTime : 0L * unit : 秒 * workQueue : LinkedBlockingQueue **/ return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
newFixedThreadPool和SingleThreadExecutor类似,唯一的区别就是核心线程数不同,并且由于使用的是LinkedBlockingQueue。LinkedBlockingQueue默认的最大任务数量是Integer.MAX_VALUE,非常大,可以理解为无限大吧;但是存在这种情况,当每个线程获取到一个任务后,执行时间比较长,导致workQueue里积压的任务越来越多,机器的内存使用不停的飙升,最后也会导致OOM。
jdk7提供了7个阻塞队列,分别是:
ArrayBlockingQueue:一个由数组结构组成的有界阻塞队列
LinkedBlockingQueue:一个由链表结构组成的有界阻塞队列
PriorityBlockingQueue:一个支持优先级排序的无界阻塞队列
DelayQueue:一个使用优先级队列实现的无界阻塞队列
SynchronousQueue:一个不存储元素的阻塞队列
LinkedTransferQueue:一个由链表结构组成的无界阻塞队列
LinkedBlockingDueue:一个 由链表结构组成的双向阻塞队列
线程池工作原理图解:
边栏推荐
- AdguardHome如何配置设置?我的AdguardHome配置内容过滤器拦截列表
- Coroutines and Lifecycle in Kotlin
- 小程序插件让开发者受益的几个理由
- Question about #oracle#, how to solve it?
- MySql模糊查询大全
- 21天学习挑战赛--第一天打卡(屏幕密度)
- LayaBox---TypeScript---Mixins
- MSYS2 QtCreator Clangd code analysis can not find mm_malloc.h problem remedy
- 放苹果(暑假每日一题 13)
- Getting Started with Three.JS Programmatic Modeling
猜你喜欢
随机推荐
idea常用插件
情景剧《重走长征路》上演
从测试入门到测试架构师,这10年,他是这样让自己成才的
FPGA手撕代码——CRC校验码的多种Verilog实现方式 (2021乐鑫科技数字IC提前批代码编程)
365天挑战LeetCode1000题——Day 047 设计循环队列 循环队列
sva 断言资料
21 Days Learning Challenge - Day 1 Punch (Screen Density)
Oracle查询提示 ORA-00933 SQL command not properly ended 原因排查
注意力机制
Several reasons why applet plugins benefit developers
ansible模块--copy模块
LayaBox---TypeScript---Module
有奖征集|TaoCloud&宝德联合举办全闪POC!
循环语句综合练习
X86函数调用模型分析
21天学习挑战赛--第一天打卡(屏幕密度)
LayaBox---TypeScript---Iterator and generator
Getting Started with Three.JS Programmatic Modeling
LayaBox---TypeScript---JSX
Excel dynamic chart production