Android 中的 线程池

    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

    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

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

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

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

三、newCachedThreadPool

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

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

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

四、ScheduledThreadPoolExecutor

    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());
    }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值