多线程笔记
在创建线程池的时候不管你创建的是newFixedThreadPool,还是newCachedThreadPool,还是newSingleThreadPool
在里面调用的都是new ThreadPoolExecutor这个类,知识传入的参数值不同而已
实例:
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}
默认情况下,线程池中并没有任何线程,而是等待有任务到来才创建线程去执行任务
public
class
ThreadPoolExecutor
extends
AbstractExecutorService {
.....
public
ThreadPoolExecutor(
int
corePoolSize,
int
maximumPoolSize,
long
keepAliveTime,TimeUnit unit,
BlockingQueue<Runnable> workQueue);
public
ThreadPoolExecutor(
int
corePoolSize,
int
maximumPoolSize,
long
keepAliveTime,TimeUnit unit,
BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory);
public
ThreadPoolExecutor(
int
corePoolSize,
int
maximumPoolSize,
long
keepAliveTime,TimeUnit unit,
BlockingQueue<Runnable> workQueue,RejectedExecutionHandler handler);
public
ThreadPoolExecutor(
int
corePoolSize,
int
maximumPoolSize,
long
keepAliveTime,TimeUnit unit,
BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler);
...
}