Android线程池的使用

本文介绍了线程池的概念及其在Android中的应用。详细解释了ThreadPoolExecutor的构造方法及参数含义,并对比了FixedThreadPool、CachedThreadPool、ScheduledThreadPool和SingleThreadExecutor的特点。
为什么要使用线程池
  • 1.重用线程池中的线程,避免线程的创建和销毁带来的性能开销;
  • 2.能有效的控制线程池中的线程并发数,避免大量线程之间互相抢占资源导致阻塞;
  • 3.能对线程进行简单的管理,并提供定时执行或间隔循环执行任务;

ThreadPoolExecutor
Android中的线程池概念是来源于java中Executor,Executor是一个空接口,真正的线程池实现是ThreadPoolExecutor。先看下它的构造方法:

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             threadFactory, defaultHandler);
    }

各参数的含义:

  • corePoolSize:线程池核心线程数,默认情况下,核心线程会在线程池中一直存在,即使处于闲置状态。当我们把ThreadPoolExecutor中的allowCoreThreadTimeOut属性设置为true,那么闲置的核心线程在等待新任务时,如果超过了KeepAliveTime所设置的时间,核心线程也将被回收。
  • maximumPoolSize:线程池能容纳的最大线程数,当线程池中的线程达到这个数后,新任务将会被阻塞。
  • keepAliveTime:非核心线程数闲置的时间。
  • unit:指定keepAliveTime的时间单位。
  • workQueue:线程池中的任务队列。
  • threadFactory:线程工厂,为线程池提供创建新线程的功能。
Android中常用的线程池有四种:

FixedThreadPool、CachedThreadPool、ScheduledThreadPool、SingleThreadExecutor。

FixedThreadPool
FixedThreadPool线程池是通过Executors的newFixedThreadPool方法来创建的。它的特点是线程中的线程数量固定。即使线程处于闲置状态也不会回收,除非线程池被关闭。当所有的线程都处于活跃状态的时候,新任务就处于队列中等待线程来处理。并且FixedThreadPool只有核心线程,没有非核心线程。

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {         
return new ThreadPoolExecutor(nThreads,
                              nThreads, 
                              0L,
                              TimeUnit.MILLISECONDS,                                      
                              new LinkedBlockingQueue<Runnable>(),
                               threadFactory);
     }

CachedThreadPool
CachedThreadPool线程池是通过Executors的newCachedThreadPool方法创建的。它是一种线程数不固定的线程池,它没有核心线程,只有非核心线程,当线程池中的线程都处于活跃状态时,就会创建新的线程来处理新的任务。否则就会利用闲置线程来处理新任务。线程池中的线程都有超时机制,超时时间是60s,只有一个线程闲置超过这个时间就会被回收。所以这种线程池适合处理大量并且耗时较少的任务。

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

ScheduledThreadPool
ScheduledThreadPool线程池是通过Executors的newScheduledThreadPool创建的。它的核心线程数是固定的,但是非核心线程数不固定,并且当非核心线程一处于闲置状态,就会被回收。这种线程池适合执行定时任务和具有固定周期的重复任务。

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }


public ScheduledThreadPoolExecutor(int corePoolSize,ThreadFactory threadFactory) {
        super(corePoolSize, Integer.MAX_VALUE,
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
              new DelayedWorkQueue(), threadFactory);
    }

SingleThreadExecutor
SingleThreadExecutor线程池是通过Executors的newSingleExecutor方法来创建的,这类线程池中只有一个核心线程,没有非核心线程,这就确保了所有任务都是在同一线程按顺序执行的。

public static ExecutorService newSingleThreadExecutor() {

        return new FinalizableDelegatedExecutorService
                  (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>()));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值