# Java的线程池
new ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
FixedThredPool: new ThreadExcutor(n, n, 0L, ms, new LinkedBlockingQueue<Runable>()
SingleThreadExecutor: new ThreadExcutor(1, 1, 0L, ms, new LinkedBlockingQueue<Runable>())
CachedTheadPool: new ThreadExcutor(0, max_valuem, 60L, s, new SynchronousQueue<Runnable>());
ScheduledThreadPoolExcutor: ScheduledThreadPool, SingleThreadScheduledExecutor.
public void execute(Runnable command) {
// command为null,抛出NullPointerException
if (command == null)
throw new NullPointerException();
int c = ctl.get();
// 线程池中的线程数小于corePoolSize,创建新的线程
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))// 创建工作线程
return;
c = ctl.get();
}
// 将任务添加到阻塞队列,如果
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}// 阻塞队列已满,尝试创建新的线程,如果超过maximumPoolSize,执行handler.rejectExecution()
else if (!addWorker(command, false))
reject(command);
}
protected void beforeExecute(Thread t, Runnable r) { }
1. 讲讲Java的线程池
2. 具体的场景,如果corePoolSize为x,maximumPoolSize为y,阻塞队列为z,第w个任务进来如何分配?
3. 线程池如何进行调优?
4. 线程池中的核心参数,超过核心size怎么处理,队列满怎么处理,拒绝策略有哪些?(比较具体)