[Android] in-depth understanding of Android: using a thread pool of Principle Analysis

This column focuses a large share knowledge Bat interview, follow-up will continue to update, like it trouble clicking a concern

1 Introduction

Thread pool can simply be seen as a collection of threads by using the thread pool, we can easily reuse threads to avoid the overhead of creating and destroying threads frequently brought. In the application, the thread pool can be used in the back-end related services. Such as Web servers, database servers, and so on. A Web server, for example, if the Web server receives a large number of short-HTTP requests, if at this time we simply create a processing thread for each HTTP request, the server's resources will soon be exhausted. Of course, we also can own to manage and reuse the thread has been created in order to limit the consumption of resources, but it will use the program logic becomes complicated. Fortunately, fortunately, we do not have to do that. In JDK 1.5, the official has provided a powerful tool thread pool class. By using these tools, we can use multi-threading technology with low price.

Java thread pool as an important tool for concurrent classes, will be used on the basis of, I think it is necessary to learn about the relevant principles of the thread pool. After all, in addition to the thread pool thread management, but also the management tasks, but also has statistical functions. So to understand a bit, it can still expand their horizons, but also can be more familiar with the thread pool.

2. inheritance system

Thread pool involved is not a lot of interfaces and classes that inherit the system is relatively simple. Related to inheritance are as follows:

As shown above, the top level interface Executor only a single method execute. ExecutorService parent class interfaces based on the interface, including but not limited to the statement shutdown, submit, invokeAll, invokeAny or the like. As ScheduledExecutorService interface, it is to declare a number of tasks related to the timing and methods, such as  scheduleand scheduleAtFixedRate. The core thread pool is achieved in ThreadPoolExecutor class, we use Executors calls newFixedThreadPool, newSingleThreadExecutorand newCachedThreadPoolother methods to create a thread pool are ThreadPoolExecutor type.

The above is a brief introduction of the inherited system thread pool, here let everyone have some understanding of the broad contours of the thread pool. Next, I will introduce the thread pool implementation principle, continue to read on.

3. Principle Analysis

3.1 Analysis of the core parameters

3.1.1 Introduction to the core parameters

The above mentioned section, the core thread pool implementation that is ThreadPoolExecutor class. Core class contains several attributes can be initialized in the constructor. Before introducing the core attributes, we take a look at ThreadPoolExecutor constructor, as follows:

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                          RejectedExecutionHandler handler)

  

As shown above, that is the core argument constructor parameters, here I use a table to briefly explain the significance of each parameter. as follows:

parameter Explanation
corePoolSize The core number of threads. When the value is less than the number of threads, thread pool will give priority to create a new thread to perform new tasks
maximumPoolSize The maximum number of threads in the thread pool can be maintained
keepAliveTime Idle thread survival time
workQueue Task queue for tasks performed by a cache
threadFactory Thread factory. It can be set by the factory more meaningful name for the new thread
handler Rejection policy. When the task queue and thread pool are at saturation point, refused to use strategies to handle new tasks. The default is AbortPolicy, namely direct throw an exception

These are the brief introduction of each parameter, I will be described in detail below for some parameters, read on.

3.1.2 Thread creation rule

在 Java 线程池实现中,线程池所能创建的线程数量受限于 corePoolSize 和 maximumPoolSize 两个参数值。线程的创建时机则和 corePoolSize 以及 workQueue 两个参数有关。下面列举一下线程创建的4个规则(线程池中无空闲线程),如下:

  1. 线程数量小于 corePoolSize,直接创建新线程处理新的任务

  2. 线程数量大于等于 corePoolSize,workQueue 未满,则缓存新任务

  3. 线程数量大于等于 corePoolSize,但小于 maximumPoolSize,且 workQueue 已满。则创建新线程处理新任务

  4. 线程数量大于等于 maximumPoolSize,且 workQueue 已满,则使用拒绝策略处理新任务

简化一下上面的规则:

序号 条件 动作
1 线程数 < corePoolSize 创建新线程
2 线程数 ≥ corePoolSize,且 workQueue 未满 缓存新任务
3 corePoolSize ≤ 线程数 < maximumPoolSize,且 workQueue 已满 创建新线程
4 线程数 ≥ maximumPoolSize,且 workQueue 已满 使用拒绝策略处理

3.1.3 资源回收

考虑到系统资源是有限的,对于线程池超出 corePoolSize 数量的空闲线程应进行回收操作。进行此操作存在一个问题,即回收时机。目前的实现方式是当线程空闲时间超过 keepAliveTime 后,进行回收。除了核心线程数之外的线程可以进行回收,核心线程内的空闲线程也可以进行回收。回收的前提是allowCoreThreadTimeOut属性被设置为 true,通过public void allowCoreThreadTimeOut(boolean) 方法可以设置属性值。

3.1.4 排队策略

如3.1.2 线程创建规则一节中规则2所说,当线程数量大于等于 corePoolSize,workQueue 未满时,则缓存新任务。这里要考虑使用什么类型的容器缓存新任务,通过 JDK 文档介绍,我们可知道有3中类型的容器可供使用,分别是同步队列有界队列无界队列。对于有优先级的任务,这里还可以增加优先级队列。以上所介绍的4中类型的队列,对应的实现类如下:

实现类 类型 说明
SynchronousQueue 同步队列 该队列不存储元素,每个插入操作必须等待另一个线程调用移除操作,否则插入操作会一直阻塞
ArrayBlockingQueue 有界队列 基于数组的阻塞队列,按照 FIFO 原则对元素进行排序
LinkedBlockingQueue 无界队列 基于链表的阻塞队列,按照 FIFO 原则对元素进行排序
PriorityBlockingQueue 优先级队列 具有优先级的阻塞队列

3.1.5 拒绝策略

如3.1.2 线程创建规则一节中规则4所说,线程数量大于等于 maximumPoolSize,且 workQueue 已满,则使用拒绝策略处理新任务。Java 线程池提供了4中拒绝策略实现类,如下:

实现类 说明
AbortPolicy 丢弃新任务,并抛出 RejectedExecutionException
DiscardPolicy 不做任何操作,直接丢弃新任务
DiscardOldestPolicy 丢弃队列队首的元素,并执行新任务
CallerRunsPolicy 由调用线程执行新任务

以上4个拒绝策略中,AbortPolicy 是线程池实现类所使用的策略。我们也可以通过方法public void setRejectedExecutionHandler(RejectedExecutionHandler)修改线程池决绝策略。

3.2 重要操作

3.2.1 线程的创建与复用

在线程池的实现上,线程的创建是通过线程工厂接口ThreadFactory的实现类来完成的。默认情况下,线程池使用Executors.defaultThreadFactory()方法返回的线程工厂实现类。当然,我们也可以通过

public void setThreadFactory(ThreadFactory)方法进行动态修改。具体细节这里就不多说了,并不复杂,大家可以自己去看下源码。

在线程池中,线程的复用是线程池的关键所在。这就要求线程在执行完一个任务后,不能立即退出。对应到具体实现上,工作线程在执行完一个任务后,会再次到任务队列获取新的任务。如果任务队列中没有任务,且 keepAliveTime 也未被设置,工作线程则会被一致阻塞下去。通过这种方式即可实现线程复用。

说完原理,再来看看线程的创建和复用的相关代码(基于 JDK 1.8),如下:

+----ThreadPoolExecutor.Worker.java
Worker(Runnable firstTask) {
    setState(-1);
    this.firstTask = firstTask;
    // 调用线程工厂创建线程
    this.thread = getThreadFactory().newThread(this);
}

// Worker 实现了 Runnable 接口
public void run() {
    runWorker(this);
}

+----ThreadPoolExecutor.java
final void runWorker(Worker w) {
    Thread wt = Thread.currentThread();
    Runnable task = w.firstTask;
    w.firstTask = null;
    w.unlock();
    boolean completedAbruptly = true;
    try {
        // 循环从任务队列中获取新任务
        while (task != null || (task = getTask()) != null) {
            w.lock();
            // If pool is stopping, ensure thread is interrupted;
            // if not, ensure thread is not interrupted.  This
            // requires a recheck in second case to deal with
            // shutdownNow race while clearing interrupt
            if ((runStateAtLeast(ctl.get(), STOP) ||
                 (Thread.interrupted() &&
                  runStateAtLeast(ctl.get(), STOP))) &&
                !wt.isInterrupted())
                wt.interrupt();
            try {
                beforeExecute(wt, task);
                Throwable thrown = null;
                try {
                    // 执行新任务
                    task.run();
                } catch (RuntimeException x) {
                    thrown = x; throw x;
                } catch (Error x) {
                    thrown = x; throw x;
                } catch (Throwable x) {
                    thrown = x; throw new Error(x);
                } finally {
                    afterExecute(task, thrown);
                }
            } finally {
                task = null;
                w.completedTasks++;
                w.unlock();
            }
        }
        completedAbruptly = false;
    } finally {
        // 线程退出后,进行后续处理
        processWorkerExit(w, completedAbruptly);
    }
}

  

3.2.2 提交任务

通常情况下,我们可以通过线程池的submit方法提交任务。被提交的任务可能会立即执行,也可能会被缓存或者被拒绝。任务的处理流程如下图所示:

上面的流程图不是很复杂,下面再来看看流程图对应的代码,如下:

+---- AbstractExecutorService.java
public Future<?> submit(Runnable task) {
    if (task == null) throw new NullPointerException();
    // 创建任务
    RunnableFuture<Void> ftask = newTaskFor(task, null);
    // 提交任务
    execute(ftask);
    return ftask;
}

+---- ThreadPoolExecutor.java
public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();

    int c = ctl.get();
    // 如果工作线程数量 < 核心线程数,则创建新线程
    if (workerCountOf(c) < corePoolSize) {
        // 添加工作者对象
        if (addWorker(command, true))
            return;
        c = ctl.get();
    }
    
    // 缓存任务,如果队列已满,则 offer 方法返回 false。否则,offer 返回 true
    if (isRunning(c) && workQueue.offer(command)) {
        int recheck = ctl.get();
        if (! isRunning(recheck) && remove(command))
            reject(command);
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
    
    // 添加工作者对象,并在 addWorker 方法中检测线程数是否小于最大线程数
    else if (!addWorker(command, false))
        // 线程数 >= 最大线程数,使用拒绝策略处理任务
        reject(command);
}

private boolean addWorker(Runnable firstTask, boolean core) {
    retry:
    for (;;) {
        int c = ctl.get();
        int rs = runStateOf(c);

        // Check if queue empty only if necessary.
        if (rs >= SHUTDOWN &&
            ! (rs == SHUTDOWN &&
               firstTask == null &&
               ! workQueue.isEmpty()))
            return false;

        for (;;) {
            int wc = workerCountOf(c);
            // 检测工作线程数与核心线程数或最大线程数的关系
            if (wc >= CAPACITY ||
                wc >= (core ? corePoolSize : maximumPoolSize))
                return false;
            if (compareAndIncrementWorkerCount(c))
                break retry;
            c = ctl.get();  // Re-read ctl
            if (runStateOf(c) != rs)
                continue retry;
            // else CAS failed due to workerCount change; retry inner loop
        }
    }

    boolean workerStarted = false;
    boolean workerAdded = false;
    Worker w = null;
    try {
        // 创建工作者对象,细节参考上一节所贴代码
        w = new Worker(firstTask);
        final Thread t = w.thread;
        if (t != null) {
            final ReentrantLock mainLock = this.mainLock;
            mainLock.lock();
            try {
                int rs = runStateOf(ctl.get());
                if (rs < SHUTDOWN ||
                    (rs == SHUTDOWN && firstTask == null)) {
                    if (t.isAlive()) // precheck that t is startable
                        throw new IllegalThreadStateException();
                    // 将 worker 对象添加到 workers 集合中
                    workers.add(w);
                    int s = workers.size();
                    // 更新 largestPoolSize 属性
                    if (s > largestPoolSize)
                        largestPoolSize = s;
                    workerAdded = true;
                }
            } finally {
                mainLock.unlock();
            }
            if (workerAdded) {
                // 开始执行任务
                t.start();
                workerStarted = true;
            }
        }
    } finally {
        if (! workerStarted)
            addWorkerFailed(w);
    }
    return workerStarted;
}

  

上面的代码略多,不过结合上面的流程图,和我所写的注释,理解主逻辑应该不难。

3.2.3 关闭线程池

我们可以通过shutdownshutdownNow两个方法关闭线程池。两个方法的区别在于,shutdown 会将线程池的状态设置为SHUTDOWN,同时该方法还会中断空闲线程。shutdownNow 则会将线程池状态设置为STOP,并尝试中断所有的线程。中断线程使用的是Thread.interrupt方法,未响应中断方法的任务是无法被中断的。最后,shutdownNow 方法会将未执行的任务全部返回。

调用 shutdown 和 shutdownNow 方法关闭线程池后,就不能再向线程池提交新任务了。对于处于关闭状态的线程池,会使用拒绝策略处理新提交的任务。

4.几种线程池

一般情况下,我们并不直接使用 ThreadPoolExecutor 类创建线程池,而是通过 Executors 工具类去构建线程池。通过 Executors 工具类,我们可以构造5中不同的线程池。下面通过一个表格简单介绍一下几种线程池,如下:

静态构造方法 说明
newFixedThreadPool(int nThreads) 构建包含固定线程数的线程池,默认情况下,空闲线程不会被回收
newCachedThreadPool() 构建线程数不定的线程池,线程数量随任务量变动,空闲线程存活时间超过60秒后会被回收
newSingleThreadExecutor() 构建线程数为1的线程池,等价于 newFixedThreadPool(1) 所构造出的线程池
newScheduledThreadPool(int corePoolSize) 构建核心线程数为 corePoolSize,可执行定时任务的线程池
newSingleThreadScheduledExecutor() 等价于 newScheduledThreadPool(1)

关于我

更多信息可以点击关于我 , 非常希望和大家一起交流 , 共同进步

 

Guess you like

Origin www.cnblogs.com/1157760522ch/p/11593171.html