Java并发编程之线程池简介
文章目录
目录
前言
Java线程池有Executors方式创建线程池和通过ThreadPoolExecutor指定参数的方式来创建线程池,两种方式各有不同,通过Executors方式创建的线程池为某些参数固定,然而为了方便线程的参数自定义,通常使用ThreadPoolExecutor方式来创建和管理线程池。本文主要分享线程池种类,线程池几种构造方法,重要的核心参数介绍。更多源码级别的讲解会在后续章节。
一、Executors线程池的种类
1.newFixedThreadPool
固定线程数量的线程池,通过Executors.newFixedThreadPool(int nThreads)方式来创建
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
2.newSingleThreadExecutor
单线程线程池 Executors.newSingleThreadExecutor() 核心线程数为1,最大线程数为1
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
3.newCachedThreadPool
可缓存的线程池,如果线程池的容量超过了任务数,自动回收空闲线程,任务增加时可以自动添加新线程,线程池的容量不限制。比较适合处理执行时间比较小的任务。
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
4.newScheduledThreadPool
定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。适用于需要多个后台线程执行周期任务的场景。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
5.newWorkStealingPool
JDK1.8引入的线程池,使用了ForkJoinPool来作为实现。
创建一个工作窃取线程池,维护足够的线程以支持给定的并行度级别,并且可以使用多个队列来减少争用。
并行度级别对应于主动参与或可用于参与任务处理的最大线程数。线程的实际数量可能会动态增长和收缩。工作窃取池不保证提交任务的执行顺序。一个拥有多个任务队列的线程池,可以减少连接数,创建当前可用cpu数量的线程来并行执行。
public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool
(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
二、ThreadPoolExecutor
1.四种构造方法
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), handler);
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
threadFactory, defaultHandler);
}
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.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
2.线程池主要参数介绍
corePoolSize :核心线程数
maxinumPoolSize :最大线程数
keepAliveTime :非核心线程存活时间
TimeUinit :时间单位
BlockingQueue<Runnable> :阻塞队里;常见的如 ArrayBlockingQueue,LinkedBlockingQueue
DelayQueue(延迟队列),PriorityBlockingQueue(优先级阻塞队列)
ThreadFactory :线程工厂
RejectedExecutionHandler :拒绝执行策略,AbortPolicy(拒绝抛出异常)、DiscardPolicy(丢弃不处理)、DiscardOldestPolicy(丢弃队列中最前面,最老的任务,再执行任务)、CallerRunsPolicy(调用线程来执行任务)
3.线程池原理
线程池基本原理如图所示。
总结
整理分享常用的线程数种类,线程池的构造方法种类,线程池中的核心参数介绍。
更多线程池的源码分享,请查看后续章节。