- 先来看一下类构成
public interface Executor { //顶级接口Executor,定义了线程执行的方法
void execute(Runnable command);
}
public interface ExecutorService extends Executor {
//定义了线程池周期相关的方法
void shutdown(); //平缓的关闭:不再接受新的任务,同时等待已经提交的任务的完成(包括还未开始执行的任务)
List shutdownNow(); //粗暴的关闭,尝试取消正在执行的任务,同时不再继续执行提交但未开始的任务
boolean isShutdown(); //查看该执行器是否关闭
boolean isTerminated(); //查询线程池是否终止
boolean awaitTermination(long timeout, TimeUnit unit) //等待线程池达到终止状态,通常在此方法后调用shutdown()达到同步关闭线程池的效果
throws InterruptedException;
Future submit(Callable task); //submit()方法用来提交带有返回值的任务
Future submit(Runnable task, T result);
Future submit(Runnable task);
List> invokeAll(Collectionextends Callable> tasks) //用来提交复数的任务
throws InterruptedException;
List> invokeAll(Collectionextends Callable> tasks,
long timeout, TimeUnit unit)
throws InterruptedException;
T invokeAny(Collectionextends Callable> tasks)
throws InterruptedException, ExecutionException;
T invokeAny(Collectionextends Callable> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
- ThreadPoolExecutor为线程池的实现
public ThreadPoolExecutor(int corePoolSize, //核心线程数量,代表在空闲状态下线程池中维持的线程的数量
int maximumPoolSize, //线程池所能允许的最大线程数量
long keepAliveTime, //空闲线程的存活时间
TimeUnit unit, //时间单位
BlockingQueue workQueue, //用来暂时保存任务的队列
ThreadFactory threadFactory, //用来创建线程
RejectedExecutionHandler handler) { //拒绝策略,在任务无法加入到队列是发挥作用
}
- 拒绝策略
(1)AbortPolicy: 该策略抛出RejectedExecutionException,调用者捕获异常后进行处理。
(2)DiscartPolicy:当新任务无法加入到队列中是,该策略悄悄抛弃该任务。
(3)DiscardOldestPolicy:该策略将抛弃即将要执行的任务。
(4)CallerRunsPolicy:该策略将某些任务回退到调用者。
- 四种类型的线程池(通用线程池的方法定义在Executors类中)
当线程池的所有参数有效设置时提交任务的完整过程:查看当前线程数量是否大于corePoolSize,若小于则创建一个新的线程来执行该任务,否则将该任务加入到workQueue中;若workerQueue也满,那么查看线程的数量是否小于maximumPoolSize,若小于则创建新的线程来执行,否则执行抛弃策略。另外若线程空闲时间大于keepAliveTime,则销毁线程。
public ThreadPoolExecutor(int corePoolSize, //通用构造方法
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler); //使用默认的线程工厂和默认的拒绝策略
}
private static final RejectedExecutionHandler defaultHandler = //默认为AbortPolicy new AbortPolicy();
undefined
(1)newFixedThreadPool :创建一个固定大小的线程池
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads, //核心数量=最大数量,keepAliveTime=0(若无以外线程将一直存在),workQueue使用LinkedBlockingQueue
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { //另一个构造方法,可指定线程工厂 return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), threadFactory);}
undefined
undefined
(2)newCachedThreadPool:创建一个可更具需要的线程池
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, //核心大小=0,最大数量不限,存活时间为60s(若长时间没有任务则该线程池为空),使用SynchronousQueue作为workeQueue
60L, TimeUnit.SECONDS,
new SynchronousQueue());
}
(3)newScheduledThreadPool:创建一个可延迟执行任务的线程池
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, //指定核心大小,不限最大数量,线程不会过期,使用DelayedWordeQueue作为workeQueue
new DelayedWorkQueue());
}
(4)newSingleThreadPool:创建一个只有一个线程的线程池
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1, //核心数量与最大数量都为1,存活时间不限,使用LinkedBlockingQueue作为workQueue
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue()));
}
Original: https://www.cnblogs.com/liwangcai/p/11884690.html
Author: 神奇海螺。
Title: java线程池的介绍与使用(Executor框架)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/574162/
转载文章受原作者版权保护。转载请注明原作者出处!