ThreadPoolExecutor的BlockingQueue<Runnable> workQueue参数详解

ThreadPoolExecutor通过BlockingQueue<Runnable> workQueue参数管理待执行的任务。SynchronousQueue是无存储的同步队列,LinkedBlockingQueue是无界队列,可能导致OOM,而ArrayBlockingQueue是有界队列,队列满时新任务会等待。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先说下new ThreadPoolExecutor是我们创建线程池的一种方式,在创建时需要设置多个参数,

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory) 

今天着重说下BlockingQueue<Runnable> workQueue工作队列参数。

该队列是当核心线程没有空闲时,再来的请求放入队列中先保存任务,不同的队列有着不同的工作方式。

像newCachedThreadPool使用的是SynchronousQueue<Runnable>

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>(),
                                  threadFactory);
}
像newFixedThreadPool使用的LinkedBlockingQueue<Runnable>
public static ExecutorService new
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值