Android 线程池

线程池概念

源于Java的Executor接口,通过ThreadPoolExceutor进行实现,而ThreadPoolExecutor继承于AbstractExecutorService,AbstractExecutorService 是ExecutorService的实现,ExecutorService继承了Executor接口.

线程池的优点

1,重用线程池中的线程,避免的频繁创建和销毁线程所带来的内存开销

2,有效的控制最大的并发数,避免线程之前因抢占资源而阻塞

3,简单管理线程,提供定时执行及指定时间间隔循环执行

常用构造方法参数

public ThreadPoolExecutor(int corePoolSize,    

                                            int maximumPoolSize,

                                            long keepAliveTime,

                                            TimeUnit unit,

                                            BlockingQueue workQueue,

                                            ThreadFactory threadFactory){}

corePoolSize,核心线程数(默认情况下一直存活于线程池,即使处于闲置),如果ThreadPoolExecutor中allowCoreThreadTimeOut

为true,则核心线程在等待新任务是有超时终止策略,超时时间由keepAliveTime决定

maximumPoolSize 最大线程数,当线程数超过这个值,则新任务将被阻塞

keepAliveTime 非核心线程超时时长,超过这个时间讲被回收,当核心线程设置了AllowCoreThreadTimeOut为true时,keepAliveTime同样适用于核心线程

unit,指定KeepAliveTime参数的时间单位

workQueue,线程池中任务队列,存储新提交的Runnable对象

threadFactory,线程工厂,为线程池创建新线程

handler,不常用参数,类型为RejectedExecutionHandler,当线程池无法执行新任务时间,会调用Handler抛出异常

执行规则

1)线程池中数量未达到核心线程数,则直接启动核心线程

2)线程池中线程数超过核心线程数量,则被插入到任务队列中排队等待执行

3)第二步中无法插入新任务,说明队列已满,如未达到规定最大线程数量,则启动非核心线程执行

4)如果线程数量超过最大值,则拒绝任务并通用RejectedExecutionHandler通知调用者

线程池主要分为四类

1,FixedThreadPool

特点

1)线程数量固定且都是核心线程,核心线程数量和最大线程数量都是nThreads

2)核心线程不会被回收,快速响应外界请求

3)没有超时机制,任务队列没有大小限制

4)新任务使用核心线程处理,没有空闲核心线程,则排队等待执行

public static ExecutorService newFixedThreadPool(int nThreads){

        return new ThreadPoolExecutor(nThreads,nThreads,

                                                            0L,TimeUnit.MILLISECONDS,

                                                            new LinkedBlockingQueue());

}

2,CachedThreadPool

特点

1)线程数量不定,只有非核心线程,最大线程数任意大,传入核心线程数量为0,最大线程数为Integer.MAX_VALUE;

2)新任务来时使用空闲线程执行,没有空闲线程则创建新线程处理

3)每个空闲线程都有超时机制,时长60s,空闲线程超过60时,则被回收

4)适合执行大量耗时较少的任务,

public static ExecutorService newCacheThreadPool(){

        return new ThreadPoolExecutor(0,Integer.MAX_VALUE,

                                                            60L,TimeUnit.SECONDS,

                                                             new SynchronusQueue());

}

3,ScheduledThreadPool

特点

1)核心线程数量固定,非核心线程数量无限

2)非核心线程闲置10s会被回收

3)主要用户执行定时任务和具有固定周期的重复任务

4)唯一有延时执行和周期重复执行能力,主要用户处理任务队列延迟的工作

    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {

        return new ScheduledThreadPoolExecutor(corePoolSize);

    }

    //默认闲置超时回收时常

    private static final long DEFAULT_KEEPALIVE_MILLIS = 10L;

    public ScheduledThreadPoolExecutor(int corePoolSize) {

        super(corePoolSize, Integer.MAX_VALUE,

              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,

              new DelayedWorkQueue());

    }

4,SingleThreadExecutor(单线程池)

特点

1)只有一个核心线程,所有任务在同一个线程池中按顺序执行

2)所有外界任务统一道一个线程中,不需要处理同步

    public static ExecutorService newSingleThreadExecutor() {

        return new FinalizableDelegatedExecutorService

            (new ThreadPoolExecutor(1, 1,

                                    0L, TimeUnit.MILLISECONDS,

                                    new LinkedBlockingQueue()));

    }

你可能感兴趣的:(Android 线程池)