线程池
线程池:
3大方法、7大参数、4种拒绝策略
3.10.1 Executors 工具类
// 创建单个线程的线程池
ExecutorService executorService = Executors.newSingleThreadExecutor();
// 创建一个固定大小的线程池
ExecutorService executorService1 = Executors.newFixedThreadPool(5);
// 创建一个弹性的线程池
ExecutorService executorService2 = Executors.newCachedThreadPool();
3.10.2 ThreadPoolExecutor 类
4种拒绝策略
// ThreadPoolExecutor 构造器
// new ThreadPoolExecutor.AbortPolicy() // 银行满了,还有人进来,不处理这个人的,抛出异常
// new ThreadPoolExecutor.CallerRunsPolicy() // 哪来的去哪里!
// new ThreadPoolExecutor.DiscardPolicy() // 队列满了,丢掉任务,不会抛出异常!
// new ThreadPoolExecutor.DiscardOldestPolicy() // 队列满了,尝试去和最早的竞争,也不会抛出异常!
public ThreadPoolExecutor(int corePoolSize, //核心线程池大小
int maximumPoolSize, //最大线程池大小
long keepAliveTime, //存活时间超时了没有人调用就会释放
TimeUnit unit, //时间单位
BlockingQueue<Runnable> workQueue, //阻塞队列
ThreadFactory threadFactory, //线程工厂
RejectedExecutionHandler handler) { //拒绝策略
// ....
}