美文网首页
Android 中的线程池

Android 中的线程池

作者: 好酸的柠檬 | 来源:发表于2018-05-24 00:54 被阅读0次

    为什么要使用线程池?


    在编程中经常会使用线程处理异步任务,如果每次执行一个任务都开一个新线程去执行,则这些线程的创建和销毁将消耗大量的资源;而且很难对线程进行控制。这时就需要线程池来对线程进行管理。线程池的优点包括:

    • 重用线程池中的线程,避免频繁的创建和销毁线程带来的系统开销;
    • 控制最大并发数,避免大量线程之间互相抢占系统资源导致阻塞;
    • 管理线程,提供定时执行以及循环执行等功能。

    Android中的线程池有哪些?它们的区别是什么?


    Android中常用的线程池主要有4种,它们直接或间接通过ThreadPoolExectuor实现的,ThreadPoolExectuor提供了一系列参数来配置线程池,下面先来了解一下ThreadPoolExectuor。

    ThreadPoolExectuor

    ThreadPoolExectuor类一共有4个构造方法,其中拥有最多参数的构造方法如下:

     public ThreadPoolExecutor(int corePoolSize,
                               int maximumPoolSize,
                               long keepAliveTime,
                               TimeUnit unit,
                               BlockingQueue<Runnable> workQueue,
                               ThreadFactory threadFactory,
                               RejectedExecutionHandler handler)
    
    • corePoolSize :核心线程数。默认情况下线程池是空的,只有任务提交时才会创建线程。如果调用线程池的prestartAllcoreThread方法,线程池会提前创建并启动所有的核心线程来等待任务。如果将ThreadPoolExecutor的allowCoreThreadTimeOut属性设置为true,那么核心线程也会有超时策略。
    • maximumPoolSize :线程池允许创建的最大线程数。
    • keepAliveTime :非核心线程闲置的超时时间。如果将ThreadPoolExecutor的allowCoreThreadTimeOut属性设置为true,那么核心线程也会有超时策略。
    • unit :keepAliveTime的时间单位。
    • workQueue :任务队列。如果当前线程数大于corePoolSize,则将任务添加到此任务队列中。该任务队列是BlockingQueue类型的,也就是阻塞队列。
    • threadFactory :线程工厂。
    • RejectedExecutionHandler :饱和策略。这是任务队列和线程池都满了时所采取的应对策略。有4种应对策略:
      • AbordPlolicy(默认):表示无法处理新任务,并抛出RejectedExcutionException。
      • CallerRunsPlicy:用调用者所在线程来处理任务。
      • AbordPlolicy:删除该任务。
      • AbordPlolicy:丢弃队列最近的任务,并执行当前任务。

    线程的处理流程如下

    线程池的处理流程.png

    线程池的种类

    通过直接或者间接地配置ThreadPoolExecutor参数可以创建这4种常用的线程池,它们分别是FixedThreadPool、CachedThreadPool、SingleThreadExecutor、SheduledThreadPool

    • FixedThreadPool

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

    FixedThreadPool只有核心线程且这些核心线程不会被回收,另外任务队列采用了无界的LinkedBlockingQueue。这意味着它能够快速响应外界的请求。
    当执行execute方法时,如果当前运行的线程未达到corePoolSize时就创建核心线程来处理任务,如果达到了核心线程数则将任务添加到LinkedBlockingQueue中,当线程池有空闲线程时,则从任务队列中去取任务执行。

    • CachedThreadPool

     public static  ExecutorService newCacheThreadPool(){
            return  new ThreadPoolExecutor(
                    0,Integer.MAX_VALUE,    //无核心线程,并且最大线程数为int的最大值.
                    60L,TimeUnit.SECONDS,  //超时时间为60s
                    new SynchronousQueue<Runnable>() //队列为SynchronousQueue同步阻塞队列,队列中没有任何容量.只有在有需求的情况下,队列中才可以试着添加任务.
            );
        }
    

    CachedThreadPool没有核心线程,非核心线程是无界的,空闲线程等待新任务的最长时间是60s,当线程池中的线程都处于活动状态时,线程池会创建新的线程来处理新任务,否则会利用空闲的线程来处理新任务。因为每次提交任务都会立即有线程去处理,所以CacheThreadPool比较适于大量的需要立即处理并且耗时较少的任务。

    • SingleThreadExecutor

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

    SingleThreadExecutor只有一个核心线程。它能够确保所有的任务都在同一个线程中按顺序执行。

    • SheduledThreadPool

     public static ScheduledExecutorService newScheduledThreadPool(int corePoolSzie) {
            return new ScheduledThreadPoolExecutor(corePoolSzie);
        }
    
        public ScheduledThreadPoolExecutor(int corePoolSize) {
            super(corePoolSize, Integer.MAX_VALUE,    //核心线程数是固定的,非核心线程无限大
                    DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,  //非核心线程有10s的空闲存活时间
                    new DelayedWorkQueue());  //DelayedWorkQueue会将任务进行排序
        }
    

    SheduledThreadPool 主要用于执行定时任务和具有固定周期的重复任务。而DelayedWorkQueue这个队列就是包装过的DelayedQueue,它会将任务进行排序,这个类的特点是在存入任务时会有一个Delay对象一起存入,代表需要过多少时间才能取出,相当于一个延时队列。当执行完成后,会再将Delay时间改成下次执行的时间并放回DelayedWorkQueue中。


    相关文章

      网友评论

          本文标题:Android 中的线程池

          本文链接:https://www.haomeiwen.com/subject/htvdjftx.html