Android多线程--Android中常见的四种线程池

来源:互联网 发布:爱淘宝买东西如何返利 编辑:程序博客网 时间:2024/06/07 10:46

前言

    使用线程池可以给我们带来很多好处,首先通过线程池中线程的重用,减少创建和销毁线程的性能开销。其次,能控制线程池中的并发数,否则会因为大量的线程争夺CPU资源造成阻塞。最后,线程池能够对线程进行管理,比如使用ScheduledThreadPool来设置延迟N秒后执行任务,并且每隔M秒循环执行一次。

    下面通过介绍线程池中的真正实现者--ThreadPoolExecutor来引出Android中的4类线程池的使用以及特性分析

凡事得靠ThreadPoolExecutor

    Executor是一个接口,它的具体实现就是ThreadPoolExecutor。

    Android中的线程池都是直接或间接通过配置ThreadPoolExecutor来实现不同特性的线程池。

    先看下ThreadPoolExecutor的一个常用构造方法,如下:

/** ThreadPoolExecutor构造参数介绍*/public ThreadPoolExecutor(//核心线程数,除非allowCoreThreadTimeOut被设置为true,否则它闲着也不会死int corePoolSize, //最大线程数,活动线程数量超过它,后续任务就会排队                   int maximumPoolSize, //超时时长,作用于非核心线程(allowCoreThreadTimeOut被设置为true时也会同时作用于核心线程),闲置超时便被回收           long keepAliveTime,                          //枚举类型,设置keepAliveTime的单位,有TimeUnit.MILLISECONDS(ms)、TimeUnit. SECONDS(s)等TimeUnit unit,//缓冲任务队列,线程池的execute方法会将Runnable对象存储起来BlockingQueue<Runnable> workQueue,//线程工厂接口,只有一个new Thread(Runnable r)方法,可为线程池创建新线程ThreadFactory threadFactory)
    ThreadPoolExecutor的各个参数所代表的特性请看注释,我们看下ThreadPoolExecutor执行任务时是如何执行的呢?(下面以currentSize表示线程池中当前线程的数量)

    (1).当currentSize<corePoolSize时,直接启动一个核心线程并执行任务。

    (2).当currentSize>=corePoolSize,并且workQueue未满时,添加进来的任务会被安排到workQueue中等待执行。

    (3).当workQueue已满,但是currentSize<maximumPoolSize时,会立即开启一个非核心线程来执行任务。

    (4).当currentSize>=corePoolSize,workQueue已满,并且currentSize>maximumPoolSize时,调用handler默认抛出RejectExecutionException异常。  

Android中的四类线程池

    Android中最常见的四类具有不同特性的线程池分别为FixThreadPool、CachedThreadPool、ScheduleTHreadPool、以及SingleThreadPool。

FixThreadPool

/**@FixThreadPool介绍*/public static ExecutorService newFixThreadPool(int nThreads){    return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());}//使用Executors.newFixThreadPool(5).execute(r);
    (1).从配置参数来看,FixThreadPool只有核心线程(corePoolSize==maximumPoolSize),并且数量固定也不会被回收(keepAliveTime==0),所有线程都活动时,因为队列没有限制大小,新任务会等待执行。

    (2).由于线程不会回收,FixThreadPool会更快地响应外界请求。

SingleThreadPool

/**@SingleThreadPool介绍*/public static ExecutorService newSingleThreadPool (int nThreads){    return new FinalizableDelegatedExecutorService ( new ThreadPoolExecutor (1, 1, 0, TimeUnit. MILLISECONDS, new LinkedBlockingQueue<Runnable>()) );}//使用Executors.newSingleThreadPool().execute(r);

    (1).从配置参数可以看出,SingleThreadPool只有一个核心线程,确保所有任务都在同一线程中按顺序完成。因此不需要处理线程同步的问题。

    (2).可以把SingleThreadPool简单的理解为FixThreadPool的参数被设置为1的情况,即Executors.newFixThreadPool(1).execute(r)。

CachedThreadPool

/**@CachedThreadPool介绍*/public static ExecutorService newCachedThreadPool(int nThreads){    return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());}//使用Executors.newCachedThreadPool().execute(r);
    (1).CachedThreadPool只有非核心线程,最大线程数非常大,所有线程都活动时,会为新任务创建新线程,否则利用空闲线程(60s空闲时间,过了就会被回收,所以线程池中有0个线程的可能)处理任务。

    (2).任务队列SynchronousQueue相对于一个空集合,导致任何任务都会被立即执行。

    (3).比较适合执行大量的耗时较少的任务。

ScheduledThreadPool

/**@ScheduledThreadPool介绍*/public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize){return new ScheduledThreadPoolExecutor(corePoolSize);}public ScheduledThreadPoolExecutor(int corePoolSize){super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedQueue ());}//使用,延迟1秒执行,每隔2秒执行一次Runnable rExecutors.newScheduledThreadPool(5).scheduleAtFixedRate(r, 1000, 2000, TimeUnit.MILLISECONDS);
    4个里面唯一一个有延迟执行和周期重复执行的线程池。

    (1).核心线程数固定,非核心线程(闲着没活干会被立即回收)数没有限制。

    (2).从上面代码看出,ScheduledThreadPool主要用于执行定时任务以及有固定周期的重复任务。

原创粉丝点击