1、通过ThreadpoolExecutor创建线程,关键的构造函数
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
PS:
corePoolSize 核心线程数;
maximumPoolSize 最大线程数
keepAliveTime 核心线程外的线程存活的时间
unit 存活时间单位
workQueue 当核心线程池满的情况下将任务存放到该队列
threadFactory 线程工程,主要负责给线程池中的线程命名
事例:
priv