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