首先说下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

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

被折叠的 条评论
为什么被折叠?



