Java通过Executors提供了四种线程池:
- newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。(线程最大并发数不可控制)
- newFixedThreadPool:创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
- newScheduledThreadPool:创建一个定长线程池,支持定时及周期性任务执行。
- newSingleThreadExecutor:创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
Executors创建四种线程的源码:
1、newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
2、newCachedThreadPool
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
3、newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
4、newScheduledThreadPool
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
我们会发现这几个java提供的线程池最终都是返回一个ThreadPoolExecutor对象。
5、ThreadPoolExecutor构造方法:
public ThreadPoolExecutor(int corePoolSize,//核心线程池大小
int maximumPoolSize,//最大线程池大小
long keepAliveTime,//线程池中超过corePoolSize数目的空闲线程最大存活时间;可以allowCoreThreadTimeOut(true)成为核心线程的有效时间
TimeUnit unit,//keepAliveTime的时间单位
BlockingQueue<Runnable> workQueue,//阻塞任务队列
ThreadFactory threadFactory,//线程工厂:用于创建线程
RejectedExecutionHandler handler) {//拒绝策略:当提交任务数超过maxmumPoolSize+workQueue之和时,任务会交给RejectedExecutionHandler来处理
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;
}
注意:
1、当线程池小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程。
2、当线程池达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中任务调度执行
3、当workQueue已满,且maximumPoolSize>corePoolSize时,新提交任务会创建新线程执行任务
4、当提交任务数超过maximumPoolSize时,新提交任务由RejectedExecutionHandler处理
5、当线程池中超过corePoolSize线程,空闲时间达到keepAliveTime时,关闭空闲线程
6、当设置allowCoreThreadTimeOut(true)时,线程池中corePoolSize线程空闲时间达到keepAliveTime也将关闭
7、RejectedExecutionHandler接口有四个实现类(四种拒绝策略)
(1)AbortPolicy:抛出RejectedExecutionException异常;
(2)、CallerRunsPolicy:它直接在 execute 方法的调用线程中运行被拒绝的任务;如果执行程序已关闭,则会丢弃该任务
(3)、DiscardPolicy:不能执行的任务将被删除;
(4)、DiscardOldestPolicy:位于工作队列头部的任务将被删除(如果失败,会重复执行)
8、默认的拒绝策略:
/**
* The default rejected execution handler
*/
private static final RejectedExecutionHandler defaultHandler =
new AbortPolicy();
合理的运用ThreadPoolExecutor,就可以自定义线程池了。
自定义线程池:单例
public class AsynTaskThreadPool {
private int corePoolSize = 2;
private int maxinumPoolSize = 10;
private long keepAliveTime = 1000L;
private TimeUnit unit = TimeUnit.SECONDS;
private int capacity = 10;
private BlockingQueue<Runnable> workQueue = null;
private ThreadFactory threadFactory = null;
private RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
private ThreadPoolExecutor exeThreadPool = null;
private static AsynTaskThreadPool asynTaskThreadPool = null;
private AsynTaskThreadPool(int corePoolSize,int maxinumPoolSize,String namePrefix, int timeoutQueueSize){
this.corePoolSize = corePoolSize > 0 ? corePoolSize : this.corePoolSize;
this.maxinumPoolSize = maxinumPoolSize > 0 ? maxinumPoolSize : this.maxinumPoolSize;
//线程安全的队列
workQueue = new ArrayBlockingQueue<>(timeoutQueueSize > 0 ? timeoutQueueSize : this.capacity);
exeThreadPool = new ThreadPoolExecutor(corePoolSize, maxinumPoolSize, timeoutQueueSize, unit, workQueue, threadFactory, handler);
}
public void execute(Runnable task){
exeThreadPool.submit(task);
}
public <T> Future<T> execute(Callable<T> task){
return exeThreadPool.submit(task);
}
public static AsynTaskThreadPool getInstance(int corePoolSize, int maximumPoolSize, String namePrefix,
int timeoutQueueSize){
asynTaskThreadPool = new AsynTaskThreadPool(corePoolSize,maximumPoolSize,namePrefix,timeoutQueueSize);
return asynTaskThreadPool;
}
static class AsynTaskThreadFactory implements ThreadFactory{
private final AtomicInteger threadNum = new AtomicInteger(0);
private String threadName = "";
@Override
public Thread newThread(Runnable r) {
Thread th = new Thread(r,threadName+threadNum.getAndIncrement());
//设置为用户线程(默认是false,为用户线程)
th.setDaemon(false);
return th;
}
}
/**
* 关闭线程池
*/
public void shutDown() {
exeThreadPool.shutdown();
}
public int getCorePoolSize() {
return corePoolSize;
}
public int getMaxinumPoolSize() {
return maxinumPoolSize;
}
public long getKeepAliveTime() {
return keepAliveTime;
}
}