主要内容

  • 为什么要使用线程池
  • 线程池的优点
  • 线程池源码剖析

为什么要使用线程池

在 Android 中,我们使用子线程来处理异步任务。但是线程不可能无限制的产生,线程的创建和销毁会都会有相应的开销,如果在一段时间内频繁的创建和销毁线程,就会导致频繁的 GC,这样就会使程序的效率降低。

同时,线程之间由于没有统一的管理,就会争抢资源,容易造成卡顿,甚至应用崩溃。

这时候如果我们采用线程池,因为线程池中会缓存一定数量的线程,可以避免频繁的创建和销毁所带来的系统开销。

线程池的优点

(1)重用线程池中的线程,避免因为线程的创建和销毁所带来的内存开销
(2)能有效控制线程池的最大并发数,避免大量的线程之间互相抢占系统资源而导致阻塞现象
(3)能对线程池进行简单的管理,并提供定时执行以及指定间隔循环执行等功能
——《Android 群英传》

线程池源码剖析

Android 中线程池的概念来源于 Java 中的 Executor。

public interface Executor {

    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the {@code Executor} implementation.
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);
}

它只是一个接口,实现要看 ThreadPoolExecutor。

ThreadPoolExecutor

android 在线程里修改 ui android创建线程池_ci


这是它的继承结构,最终实现了 Executor 接口。我们可以从它的构造方法来分析它的线程池的配置。它总共有四个构造方法,我们可以看参数最多的来找出它每个参数的含义

android 在线程里修改 ui android创建线程池_android_02

/**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
     */
    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;
    }

可以看到源码中给出了注释,那么就来看一下。

  • corePoolSize:线程池的核心线程数。默认情况,核心线程在线程池中会一直存活,如果我们将 ThreadPoolExecutor 中的 allowCoreThreadTimeOut 属性设置为 true,那么当它闲置时会有超时策略,超时时间由 keepAliveTime 决定,超出这个时间,核心线程也会被回收。默认为 false,即使闲置也会处于活动状态。如下
/**
     * If false (default), core threads stay alive even when idle.
     * If true, core threads use keepAliveTime to time out waiting
     * for work.
     */
    private volatile boolean allowCoreThreadTimeOut;
  • maximumPoolSize:线程池的最大线程数,当活动线程达到这个数之后,后续的任务就会被阻塞。
  • keepAliveTime:非核心线程的超时时长。超过这个时长,非核心线程就会被回收。非核心线程和核心线程的区别是,非核心线程只有当核心线程不够用并且线程池中有空余的时候才会被创建,执行完任务后核心线程会被销毁。
  • unit:用于指定 keepAliveTime 的时间单位,这是一个枚举。
  • workQueue:线程池中的任务队列,存储的是已被提交但是等待执行的任务(Runnable 对象)。他是通过线程池的 execute 方法提交。
  • threadFactory:为线程池提供创建新线程的功能。
public interface ThreadFactory {

    /**
     * Constructs a new {@code Thread}.  Implementations may also initialize
     * priority, name, daemon status, {@code ThreadGroup}, etc.
     *
     * @param r a runnable to be executed by new thread instance
     * @return constructed thread, or {@code null} if the request to
     *         create a thread is rejected
     */
    Thread newThread(Runnable r);
}
  • handler:拒绝策略,当线程池已被关闭,或者任务太多导致最大线程数和任务队列已经饱和,无法再接收新的任务。这时候如果再通过 execute 方法来提交任务将会被拒绝,默认情况下是一个 RejectedExecutionException。

workQueue

workQueue 是 BlockingQueue 类型,BlockingQueue 是一个特殊的队列,当我们从 BlockingQueue 中取数据时,如果 BlockingQueue 是空的,那么数据读取的操作会进入阻塞状态。当 BlockingQueue 中有了新数据时,这个取数据的操作机会被重新唤醒。同样的,如果往 BlockingQueue 存数据时,如果 BlockingQueue 已经满了,那么存数据的操作就会被阻塞,直到 BlockingQueue 中又有新的空间才会被唤醒。

  • ArrayBlockingQueue:一个规定大小的 BlockingQueue,构造函数中接收一个 int 类型的数据,代表 ArrayBlockingQueue 大小,存储的元素以 FIFO 原则(先进先出)存取。
  • LinkedBlockingQueue:一个大小不确定的 BlockingQueue,如果再构造函数中传入一个 int 数,那么它也是有大小的,如果不传,默认是 Integer.MAX_VALUE。
/**
     * Creates a {@code LinkedBlockingQueue} with a capacity of
     * {@link Integer#MAX_VALUE}.
     */
    public LinkedBlockingQueue() {
        this(Integer.MAX_VALUE);
    }
    /**
     * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
     *
     * @param capacity the capacity of this queue
     * @throws IllegalArgumentException if {@code capacity} is not greater
     *         than zero
     */
    public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        last = head = new Node<E>(null);
    }
  • PriorityBlockingQueue:和 LinkedBlockingQueue 类似,但是它不遵循 FIFO 规则,他是按照元素的 Comparator 来决定存储顺序的,这也就要求存入的数据必须实现 Comparator 接口
  • SynchronousQueue:属于线程安全的一种同步 BlockingQueue。在 SynchronousQueue 中,生产者线程的插入操作必须等待消费者线程的移除操作,SynchronousQueue 内部没有数据缓存空间,可以把它理解为无法存储元素的队列。因此我们无法对 SynchronousQueue 进行读取或者遍历其中的数据,元素只有在你试图取走的时候才会出现。可以理解为生产者和消费者一起等待,等到对方后一起离开。

拒绝策略

ThreadPoolExecutor 为 RejectedExecutionHandler 提供了几个可选值,包括 AbortPolicy,CallerRunsPolicy,DiscardPolicy,DiscardOldestPolicy。

  • AbortPolicy:默认拒绝策略,直接抛出异常
  • CallerRunsPolicy:被拒绝后,会调当前线程池所在的线程去执行被拒绝的任务,也就是调用线程。
  • DiscardPolicy:什么也没干,直接抛弃任务,既不抛异常也不执行。
  • DiscardOldestPolicy:会抛弃任务中最老的,也就是最先添加的任务,然后将被拒绝的任务加进去。
    当然,也可以自定义拒绝策略,写一个类实现 RejectedExecutionHandler 接口就可以了。

ThreadPoolExecutor 执行任务时大致遵循的规则

(1)如果线程池中的线程数量未达到核心线程的数量,那么会直接启动一个核心线程来执行任务
(2)如果线程池中的线程数量已达到或者超过核心线程的数量,那么任务会被插入到任务队列中排队等待执行
(3)如果步骤 2 中无法将任务插入到任务队列中,这往往是由于任务队列已满,这个时候如果线程数量未达到线程池规定的最大值,那么会立即启动一个非核心线程来执行任务
(4)如果步骤 3 中线程数量已经达到线程池规定的最大值,那么就拒绝执行次此任务,ThreadPoolExecutor 会通知 RejectedExecutionHandler 的 rejectedExecution 方法来通知调用者。
——《Android 群英传》

/**
 * A handler for tasks that cannot be executed by a {@link ThreadPoolExecutor}.
 *  * @since 1.5
 * @author Doug Lea
 */
public interface RejectedExecutionHandler {
    /**
     * Method that may be invoked by a {@link ThreadPoolExecutor} when
     * {@link ThreadPoolExecutor#execute execute} cannot accept a
     * task.  This may occur when no more threads or queue slots are
     * available because their bounds would be exceeded, or upon
     * shutdown of the Executor.
     *
     * <p>In the absence of other alternatives, the method may throw
     * an unchecked {@link RejectedExecutionException}, which will be
     * propagated to the caller of {@code execute}.
     *
     * @param r the runnable task requested to be executed
     * @param executor the executor attempting to execute this task
     * @throws RejectedExecutionException if there is no remedy
     */
    void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}

线程池的分类

Android 中最常见的有四类线程池。分别是 FixedThreadPool、CachedThreadPool、ScheduledThreadPool 和 SingleThreadExecutor。

  • FixedThreadPool:线程数量固定的线程池,只有核心线程并且核心线程不会被回收,所以他能更加快速的响应外界的请求。没有超时机制,任务队列也没有大小限制。
/**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * {@code nThreads} threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @return the newly created thread pool
     * @throws IllegalArgumentException if {@code nThreads <= 0}
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
  • CachedThreadPool:他是一种线程数量不定的线程池,它只有非核心线程,而且最大线程数为 Integer.MAX_VALUE,因为这个是很大的数,相当于最大线程数可以任意大。它比较适合执行大量的耗时较少的任务,当整个线程池都处于闲置状态时,线程池中的线程都会超时而被停止。
/**
     * Creates a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.  These pools will typically improve the performance
     * of programs that execute many short-lived asynchronous tasks.
     * Calls to {@code execute} will reuse previously constructed
     * threads if available. If no existing thread is available, a new
     * thread will be created and added to the pool. Threads that have
     * not been used for sixty seconds are terminated and removed from
     * the cache. Thus, a pool that remains idle for long enough will
     * not consume any resources. Note that pools with similar
     * properties but different details (for example, timeout parameters)
     * may be created using {@link ThreadPoolExecutor} constructors.
     *
     * @return the newly created thread pool
     */
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
  • ScheduledThreadPool:它的核心线程数量时固定的,但是非核心线程数时没有限制的。并且当非核心线程闲置时会立即回收。主要用于执行定时任务和具有固定周期的重复任务。
/**
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
     * given core pool size.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     */
    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE,
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
              new DelayedWorkQueue());
    }
  • SingleThreadExecutor:这个线程池内部只有一个核心线程,他确保所有的任务都在同一个线程中按顺序执行。它的意义在于统一所有的外部任务到一个线程中,使这些任务之间不需要处理线程同步问题。
/**
     * Creates an Executor that uses a single worker thread operating
     * off an unbounded queue. (Note however that if this single
     * thread terminates due to a failure during execution prior to
     * shutdown, a new one will take its place if needed to execute
     * subsequent tasks.)  Tasks are guaranteed to execute
     * sequentially, and no more than one task will be active at any
     * given time. Unlike the otherwise equivalent
     * {@code newFixedThreadPool(1)} the returned executor is
     * guaranteed not to be reconfigurable to use additional threads.
     *
     * @return the newly created single-threaded Executor
     */
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

这几个方法通过 Executors 调用。

AsyncTask

AsyncTask 内部也集成了线程池,看一下它的配置。

android 在线程里修改 ui android创建线程池_ci_03

简单使用

Runnable command = new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(2000);
            }
        };

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
        fixedThreadPool.execute(command);

        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        cachedThreadPool.execute(command);

        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(4);
        // 2000ms后执行command
        scheduledThreadPool.schedule(command,2000, TimeUnit.MILLISECONDS);
        // 延迟10ms后,每隔1000ms执行一次
        scheduledThreadPool.scheduleAtFixedRate(command,10,1000,TimeUnit.MILLISECONDS);

        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        singleThreadExecutor.execute(command);

我们也可以使用 submit 来提交任务,它的好处是 submit 有返回值。

public void submit() {
        List<Future<String>> futures = new ArrayList<>();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
        for (int i = 0; i < 10; i++) {
            Future<String> taskFuture = threadPoolExecutor.submit(new MyTask(i));
            // 将每一个任务的执行结果保存起来
            futures.add(taskFuture);
        }

        try {
            // 遍历所有任务的执行结果
            for (Future<String> future : futures) {
                Log.d("Submit", "submit : " + future.get());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    static class MyTask implements Callable<String> {

        private int taskId;

        public MyTask(int taskId) {
            this.taskId = taskId;
        }

        @Override
        public String call() throws Exception {
            SystemClock.sleep(1000);
            return "Call 方法被调用=========" + Thread.currentThread().getName() + "======" + taskId;
        }
    }

代码如上,未测试。我们可以通过实现 Callable 接口来实现异步任务,返回值即为该任务的返回值,Future 是返回结果,返回它的 isdone 属性表示异步任务执行成功。

需要注意的地方

在写代码的时候,发现阿里巴巴的 java 插件给了提示。

1. 手动创建线程池,效果会更好哦
线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。 说明:Executors 各个方法的弊端:

1)newFixedThreadPool 和 newSingleThreadExecutor:
主要问题是堆积的请求处理队列可能会耗费非常大的内存,甚至 OOM。

2)newCachedThreadPool 和 newScheduledThreadPool:
主要问题是线程数最大数是 Integer.MAX_VALUE,可能会创建数量非常多的线程,甚至 OOM。

Positive example 1:

//org.apache.commons.lang3.concurrent.BasicThreadFactory
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());

Positive example 2:

ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
        .setNameFormat("demo-pool-%d").build();

    //Common Thread Pool
    ExecutorService pool = new ThreadPoolExecutor(5, 200,
        0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    pool.execute(()-> System.out.println(Thread.currentThread().getName()));
    pool.shutdown();//gracefully shutdown

Positive example 3:

<bean id="userThreadPool"
        class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="100" />
        <property name="queueCapacity" value="2000" />

    <property name="threadFactory" value= threadFactory />
        <property name="rejectedExecutionHandler">
            <ref local="rejectedExecutionHandler" />
        </property>
    </bean>

code:

userThreadPool.execute(thread);

2. 要使用带有 ThreadFactory 参数的 ThreadPoolExecutor 构造方法哦,这样你就可以方便的设置线程名字啦。

创建线程或线程池时请指定有意义的线程名称,方便出错时回溯。创建线程池的时候请使用带 ThreadFactory 的构造函数,并且提供自定义 ThreadFactory 实现或者使用第三方实现。

ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
        .setNameFormat("demo-pool-%d").build();
    ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
        0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    singleThreadPool.execute(()-> System.out.println(Thread.currentThread().getName()));
    singleThreadPool.shutdown();



    public class TimerTaskThread extends Thread {
        public TimerTaskThread(){
        super.setName("TimerTaskThread"); …
    }

参考





《Android 开发艺术探索》