Android中的线程池的概念来源于Java中的Execuor,Executor是一个接口,真正线程池的实现为ThreadPoolExecutor。android中线程池都是直接或者间接配置ThreadPoolExecutor来实现的。
线程池的优点
- 重用线程池中的线程,避免因为线程的常见和销毁带来的性能开销。
- 能有效控制线程池的最大并发数,避免大量线程之间因为抢占系统资源而导致的阻塞现象。
- 能够对线程进行简单的管理,并提供定时执行以及指定间隔循环执行等功能。
ThreadPoolExecutor参数详解
前面说过,android中的线程池都是通过直接或者间接配置ThreadPoolExecutor的方式来实现的。那么首先来看直接配置ThreadPoolExecutor来实现线程池。
ThreadPoolExecutor的构造方法如下:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
其中,corePoolSize代表核心线程数。
maximumPoolSize代表线程池所能容纳的最大线程数。
keepAliveTime 非核心线程闲置的超时时长,超过这个时长,非核心线程会被回收。
unit 指定keepAliveTime参数的时间单位,为枚举类型,常用的有TimeUnit.MILLISECONDS(毫秒)、TimeUnit.SECONDS(秒)、TimeUnit.MINUTES(分钟)等
workQueue 线程池中的任务队列,通过线程池的execute方法提交的Runnable对象会存储在这个参数中。
当然,ThreadPoolExecutor的构建方法不止一种,还有3种,他们除了上面的参数以外,还可能有下面的参数:
threadFactory 线程工厂,为线程池提供创建新线程的功能。ThreadFactory是一个接口,它只有一个方法:Thread new Thread(Runnable r)。
handler RejectedExecutionHandler对象,当线程池无法执行新任务的时候,可能是由于任务队列以满或者是无法成功执行任务,这个时候ThreadPoolExecutor会调用handler的RejectedExecution方法来通知调用者。这里handler的可选值为:CallerRunsPolicy、AbortPolicy(默认值,直接抛出异常)、DiscardPolicy、DiscardOldestPolicy。
执行规则
- 如果线程池中的线程数量为达到核心线程数量,那么会直接启动一个核心线程来执行任务。
- 如果线程池中的线程数量已经达到或者超过核心线程的数量,那么任务就会被插入到任务队列中排队等待执行。
- 如果步骤2中无法将任务插入到任务队列中,这往往是由于任务队列已满,这个时候如果线程数量为达到线程池规定的最大值,那么会立刻启动一个非核心线程来执行任务。
- 如果步骤3中线程数量已经达到线程池规定的最大值,那么就拒绝执行任务,ThreadPoolExecutor会调用RejectedExecutionHandler的rejectedExecution方法来通知调用者。
间接配置
每次都直接配置ThreadPoolExecutor来实现线程池未免有点麻烦,android已经为我们封装好比较常用的线程池。
FixedThreadPool
实现如下:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
从其实现上我们不难看出,他是一中线程数量固定的线程池,当线程处于空闲状态时,他们并不会回收,且核心线程没有超时机制,任务队列也没有大小限制。
我们调用他也很简单:
ExecutorService executorService = Executors.newFixedThreadPool(1);
CachedThreadPool
实现如下:
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
通过分析他的实现方法,我们不难得出:
他只有非核心线程,最大线程数为Interger.MAX_VALUE,我们可以简单认为他的最大线程数没有上限,线程池空闲线程的超时时长为60s,队列也没有上限。
调用如下:
ExecutorService executorService1 = Executors.newCachedThreadPool();
ScheduledThreadPool
实现如下:
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue());
}
他的核心线程数量是固定的,非核心线程数是没有限制的,当非核心线程空闲时会被立刻回收。
调用如下:
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(8);
SingleThreadExecutor
实现如下:
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
他只有一个核心线程,所有的任务都在同一个线程中按顺序执行。他存在的意义在于统一所有外界任务到一个线程中,这使得在这些任务 之间不需处理线程同步的问题。
调用如下:
ExecutorService executorService2 = Executors.newSingleThreadExecutor();