Android 中的 线程池

java 复制代码
    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.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

1、corePoolSize 用于指定线程池的核心线程数大小;

2、maximumPoolSize 用于指定最大线程池大小。

3、keepAliveTime,unit 一起用于指定线程池中空闲线程的最大存活时间。

4、workQueue 任务队列,相当于生产者 - 消费者模式中的传输管道,用于存放待处理的任务。

5、threadFactory 用于指定创建线程的线程工厂。

6、handler 用于指定当任务队列已满且线程数量达到 maximumPoolSize 时任务的处理策略。

一、newFixedThreadPool

java 复制代码
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

核心线程数和最大线程数都是 nThreads , 所以线程池在任何时候最多也只会有 nThreads 个线程在同时运行,且在停止线程池前所有线程都不会被回收。LinkedBlockingQueue 的默认容量是 Integer.MAX_VALUE , 近乎无限,在线程繁忙的情况下有可能导致等待处理的任务持续堆积,使得系统频繁GC ,最终导致 OOM。

此类线程池适合用于希望所有任务都能够被执行的情况

二、 newSingleThreadExecutor

java 复制代码
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

newSingleThreadExecutor ,核心线程数和最大线程数都是1,所以线程池在任何时候也只能会由一个线程在同时运行,且在停止线程前所有线程都不会被回收。由于使用了 LinkedBlockingQueue ,所以在极端情况下也是有发生 OOM 的可能。

此类线程适合用于执行需要串行处理的任务,或者是任务提交间隔比任务的执行时间长的情况。

三、newCachedThreadPool

java 复制代码
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

newCachedThreadPool , 核心线程数是0,最大线程数 MAX_VALUE,所以允许同时运行的线程数量近乎无限。再加上 SynchronousQueue 是一个储存元素的阻塞队列,每当有新任务到来时,如果当前没有空闲线程的话就会马上启动一个新的线程来执行任务,这使得任务重视能够很快被执行,提升了响应速度,但同时也存在由于要执行的任务过多导致一直创建线程的可能,这在任务耗时过长且任务量过多的情况下也可能导致 OOM。

此类线程池适合用于对任务的处理速度要求比较高的情况。

四、ScheduledThreadPoolExecutor

java 复制代码
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
java 复制代码
    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE,
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
              new DelayedWorkQueue());
    }

newScheduledThreadPool方法创建的线程对应是 ScheduledThreadPoolExecutor,ScheduledThreadPoolExecutor的核心线程数由入参 corePoolSize 决定,最大线程数是 MAX_VALUE,keepAliveTime 是0秒,所以该线程池可能同时运行近乎无限的线程,但一旦当前没有待执行的任务的话,线程就会马上被回收。

此类线程适合用于需要定时多次执行特定任务的情况。

参考:

https://github.com/leavesCZY/AndroidGuide/blob/master/Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%BC%80%E5%8F%91%EF%BC%885%EF%BC%89%E8%B6%85%E8%AF%A6%E7%BB%86%E7%9A%84%20ThreadPoolExecutor%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md

相关推荐
也些宝1 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java
Gorway1 小时前
解析残差网络 (ResNet)
算法
拖拉斯旋风1 小时前
LeetCode 经典算法题解析:优先队列与广度优先搜索的巧妙应用
算法
Wect1 小时前
LeetCode 207. 课程表:两种解法(BFS+DFS)详细解析
前端·算法·typescript
Nyarlathotep01131 小时前
SpringBoot Starter的用法以及原理
java·spring boot
wuwen51 小时前
WebFlux + Lettuce Reactive 中 SkyWalking 链路上下文丢失的修复实践
java
SimonKing2 小时前
GitHub 10万星的OpenCode,正在悄悄改变我们的工作流
java·后端·程序员
Seven973 小时前
虚拟线程深度解析:轻量并发编程的未来趋势
java
雨中飘荡的记忆12 小时前
ElasticJob分布式调度从入门到实战
java·后端