转载:https://blog.csdn.net/rongxiaodong/article/details/126777087
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public class LinkedBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable {
...
/**
* Creates a {@code LinkedBlockingQueue} with a capacity of
* {@link Integer#MAX_VALUE}.
*/
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
...
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
public ThreadPoolExecutor(int corePoolSize, --核心线程数
int maximumPoolSize, --最大线程数
long keepAliveTime, --非核心线程数保留时间
TimeUnit unit, --keepAliveTime的时间单位
BlockingQueue<Runnable> workQueue, --工作队列
ThreadFactory threadFactory, --线程工厂
RejectedExecutionHandler handler --线程拒绝策略
)
//创建一个具有2个核心线程、5个最大线程,
//使用容量为10的ArrayBlockingQueue阻塞队列作为工作队列的线程池,
//使用默认的AbortPolicy拒绝策略
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
2, 5,
5, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10),
new ThreadFactoryBuilder().setNameFormat("demo-threadpool-%d").get(),
new ThreadPoolExecutor.AbortPolicy());