Java中的线程池从精通到入门

说真的,我一直认为线程池很简单,也没去看过它的实现,大概了解过其中的原理,但是并未深入学习。一方面,了解过之后很长时间不去看,非常容易忘;另一方面,还是深入源码得到的信息才会比较深刻,还能避免背书式学习。

继承结构说明

Executors中,有几个静态方法,预设了几个ThreadPoolExecutor,其实主要的区别在于new一个实例的时候,传入的参数不一样。关于这几个预设的ThreadPoolExecutor可以在了解其参数详细的定义后,再进行分析。

用法简介 - 注释

ThreadPoolExecutor的类前注释,当阅读理解随便看看,看个大概,都比网上的文章要优质。

An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.

Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

To be useful across a wide range of contexts, this class provides many adjustable parameters and extensibility hooks. However, programmers are urged to use the more convenient Executors factory methods Executors.newCachedThreadPool (unbounded thread pool, with automatic thread reclamation), Executors.newFixedThreadPool (fixed size thread pool) and Executors.newSingleThreadExecutor (single background thread), that preconfigure settings for the most common usage scenarios. Otherwise, use the following guide when manually configuring and tuning this class:

Core and maximum pool sizes

A ThreadPoolExecutor will automatically adjust the pool size (see getPoolSize) according to the bounds set by corePoolSize (see getCorePoolSize) and maximumPoolSize (see getMaximumPoolSize). When a new task is submitted in method execute(Runnable), if fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. Else if fewer than maximumPoolSize threads are running, a new thread will be created to handle the request only if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool. By setting maximumPoolSize to an essentially unbounded value such as Integer.MAX_VALUE, you allow the pool to accommodate an arbitrary number of concurrent tasks. Most typically, core and maximum pool sizes are set only upon construction, but they may also be changed dynamically using setCorePoolSize and setMaximumPoolSize.

当一个新任务通过execute()方法执行时,如果当前运行的线程数小于核心数,那么会新开一个线程处理这个任务,即使别的线程在闲置;如果当前在跑的线程数大于等于核心数但小于最大线程数,若Queue没有满,那么会将改任务加入Queue中,若Queue满了,那么会新增一个线程去处理改任务;如果大于等于最大线程数,会触发决绝策略。

On-demand construction

By default, even core threads are initially created and started only when new tasks arrive, but this can be overridden dynamically using method prestartCoreThread or prestartAllCoreThreads. You probably want to prestart threads if you construct the pool with a non-empty queue.

可以预先初始化好线程数,不用一次次execute时慢慢增加。

Creating new threads

New threads are created using a ThreadFactory. If not otherwise specified, a Executors.defaultThreadFactory is used, that creates threads to all be in the same ThreadGroup and with the same NORM_PRIORITY priority and non-daemon status. By supplying a different ThreadFactory, you can alter the thread’s name, thread group, priority, daemon status, etc. If a ThreadFactory fails to create a thread when asked by returning null from newThread, the executor will continue, but might not be able to execute any tasks. Threads should possess the “modifyThread” RuntimePermission. If worker threads or other threads using the pool do not possess this permission, service may be degraded: configuration changes may not take effect in a timely manner, and a shutdown pool may remain in a state in which termination is possible but not completed.

创建新线程是通过ThreadFactorynewThread()方法,默认实现是Executors.defaultThreadFactory。可自定义线程的名称、是否为守护进程、优先级等。

Keep-alive times

If the pool currently has more than corePoolSize threads, excess threads will be terminated if they have been idle for more than the keepAliveTime (see getKeepAliveTime(TimeUnit)). This provides a means of reducing resource consumption when the pool is not being actively used. If the pool becomes more active later, new threads will be constructed. This parameter can also be changed dynamically using method setKeepAliveTime(long, TimeUnit). Using a value of Long.MAX_VALUE TimeUnit.NANOSECONDS effectively disables idle threads from ever terminating prior to shut down. By default, the keep-alive policy applies only when there are more than corePoolSize threads, but method allowCoreThreadTimeOut(boolean) can be used to apply this time-out policy to core threads as well, so long as the keepAliveTime value is non-zero.

当线程数超过核心数,如果线程闲置超过keepAliveTime ,那么该线程将会被关闭。也可以通过 allowCoreThreadTimeOut(boolean),回收核心线程。可拓展啊,牛逼。

Queuing

Any BlockingQueue may be used to transfer and hold submitted tasks. The use of this queue interacts with pool sizing:

  • If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.
  • If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.
  • If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.

大于核心数,存储到queue;如果queue满了,新建线程,直到到达maximumPoolSize

There are three general strategies for queuing:

  • Direct handoffs. A good default choice for a work queue is a SynchronousQueue that hands off tasks to threads without otherwise holding them. Here, an attempt to queue a task will fail if no threads are immediately available to run it, so a new thread will be constructed. This policy avoids lockups when handling sets of requests that might have internal dependencies. Direct handoffs generally require unbounded maximumPoolSizes to avoid rejection of new submitted tasks. This in turn admits the possibility of unbounded thread growth when commands continue to arrive on average faster than they can be processed.
  • Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to wait in the queue when all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created. (And the value of the maximumPoolSize therefore doesn’t have any effect.) This may be appropriate when each task is completely independent of others, so tasks cannot affect each others execution; for example, in a web page server. While this style of queuing can be useful in smoothing out transient bursts of requests, it admits the possibility of unbounded work queue growth when commands continue to arrive on average faster than they can be processed.
  • Bounded queues. A bounded queue (for example, an ArrayBlockingQueue) helps prevent resource exhaustion when used with finite maximumPoolSizes, but can be more difficult to tune and control. Queue sizes and maximum pool sizes may be traded off for each other: Using large queues and small pools minimizes CPU usage, OS resources, and context-switching overhead, but can lead to artificially low throughput. If tasks frequently block (for example if they are I/O bound), a system may be able to schedule time for more threads than you otherwise allow. Use of small queues generally requires larger pool sizes, which keeps CPUs busier but may encounter unacceptable scheduling overhead, which also decreases throughput.

队列的三种策略。第一种,不让存储到到queue,一有存储动作,就新建线程,最大线程数设置成无限制;第二种,队列的长度无限,这样会导致最大线程数失效;第三种,有界队列。

Rejected tasks

New tasks submitted in method execute(Runnable) will be rejected when the Executor has been shut down, and also when the Executor uses finite bounds for both maximum threads and work queue capacity, and is saturated. In either case, the execute method invokes the RejectedExecutionHandler.rejectedExecution(Runnable, ThreadPoolExecutor) method of its RejectedExecutionHandler. Four predefined handler policies are provided:

  • In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.
  • In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.
  • In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.
  • In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)

It is possible to define and use other kinds of RejectedExecutionHandler classes. Doing so requires some care especially when policies are designed to work only under particular capacity or queuing policies.

拒绝任务发生在线程池已经处于SHUT_DOWN 状态,或最大线程数以及队列长度有限,并处于饱和状态时。预置的拒绝策略有4种,分别为:第一种,直接抛异常;第二种,调用者在自己的线程中执行该任务;第三种,直接抛弃;第四种,丢弃等待时间最久的那个任务。

自己可以自定义拒绝策略。

Hook methods

This class provides protected overridable beforeExecute(Thread, Runnable) and afterExecute(Runnable, Throwable) methods that are called before and after execution of each task. These can be used to manipulate the execution environment; for example, reinitializing ThreadLocals, gathering statistics, or adding log entries. Additionally, method terminated can be overridden to perform any special processing that needs to be done once the Executor has fully terminated.
If hook, callback, or BlockingQueue methods throw exceptions, internal worker threads may in turn fail, abruptly terminate, and possibly be replaced.

有钩子函数可以做到AOP的效果,分别在执行前,执行后。具体用途可以根据自己需求来进行自定义。

Queue maintenance

Method getQueue() allows access to the work queue for purposes of monitoring and debugging. Use of this method for any other purpose is strongly discouraged. Two supplied methods, remove(Runnable) and purge are available to assist in storage reclamation when large numbers of queued tasks become cancelled.

可获取

Reclamation

A pool that is no longer referenced in a program AND has no remaining threads may be reclaimed (garbage collected) without being explicitly shutdown. You can configure a pool to allow all unused threads to eventually die by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).

可以通过设置让线程被回收掉。

Extension example

Most extensions of this class override one or more of the protected hook methods. For example, here is a subclass that adds a simple pause/resume feature:

class PausableThreadPoolExecutor extends ThreadPoolExecutor {
    private boolean isPaused;
    private ReentrantLock pauseLock = new ReentrantLock();
    private Condition unpaused = pauseLock.newCondition();

    public PausableThreadPoolExecutor(...) { super(...); }

    protected void beforeExecute(Thread t, Runnable r) {
      super.beforeExecute(t, r);
      pauseLock.lock();
      try {
        while (isPaused) unpaused.await();
      } catch (InterruptedException ie) {
        t.interrupt();
      } finally {
        pauseLock.unlock();
      }
    }

    public void pause() {
      pauseLock.lock();
      try {
        isPaused = true;
      } finally {
        pauseLock.unlock();
      }
    }

    public void resume() {
      pauseLock.lock();
      try {
        isPaused = false;
        unpaused.signalAll();
      } finally {
        pauseLock.unlock();
      }
    }
}

线程池的状态

状态 说明
RUNNING Accept new tasks and process queued tasks
SHUTDOWN Don’t accept new tasks, but process queued tasks
STOP Don’t accept new tasks, don’t process queued tasks, and interrupt in-progress tasks
TIDYING All tasks have terminated, workerCount is zero, the thread transitioning to state TIDYING will run the terminated() hook method
TERMINATED terminated() has completed

状态变换:

  • RUNNING -> SHUTDOWN
    On invocation of shutdown()
  • (RUNNING or SHUTDOWN) -> STOP
    On invocation of shutdownNow()
  • SHUTDOWN -> TIDYING
    When both queue and pool are empty
  • STOP -> TIDYING
    When pool is empty
  • TIDYING -> TERMINATED
    When the terminated() hook method has completed

关键属性

名称 含义
corePoolSize 核心线程池大小
maximumPoolSize 最大线程池大小
keepAliveTime 线程最大空闲时间
workQueue 线程等待队列
threadFactory 线程创建工厂
handler 拒绝策略

参数最多的一个构造函数:

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;
}

项目中用到的线程池,在MDC中加入了追踪每一个请求的traceId,通过log输出:

public class SysThreadPool {

    private ThreadPoolExecutor threadPoolExecutor;

    private SysThreadPool(ThreadPoolExecutor threadPoolExecutor) {
        this.threadPoolExecutor = threadPoolExecutor;
    }

    public static void doExecute(Runnable task) {
        Map<String, String> context = MDC.getCopyOfContextMap();
        getInstance().threadPoolExecutor.execute(() -> {
            // 将父线程的MDC内容传给子线程
            if(context != null){
                MDC.setContextMap(context);
            }
            try {
                task.run();
            } catch (Exception e){
                e.printStackTrace();
            }finally {
                // 清空MDC内容
                MDC.clear();
            }
        });
    }

    private static SysThreadPool getInstance() {
        return InstanceHolder.threadPool;
    }
		// 单例模式的容器实现方式
    private static class InstanceHolder {

        private static final Integer CORE_POOL_SIZE = 50;
        private static final Integer MAX_POOL_SIZE = 500;
        private static final Long KEEP_ALIVE_TIME = 2L;
        private static final TimeUnit TIME_UNIT = TimeUnit.MINUTES;
        private static final LinkedTransferQueue QUEUE = new LinkedTransferQueue();
        private static final ThreadFactory FACTORY = new ThreadFactory() {
            private final AtomicInteger integer = new AtomicInteger();

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "SysThreadPool thread: " + integer.getAndIncrement());
            }
        };
        public static final SysThreadPool threadPool = new SysThreadPool(
                new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT, QUEUE, FACTORY));
    }
}

貌似有点问题,MAX_POOL_SIZE应该没有生效,因为队列是无界队列。

JDK预置线程池

打开Executors.java,里面很多static方法,且它的构造方法时私有的,所以这个类的用处也很明显,只是作为一个Facade。

/** Cannot instantiate. */
private Executors() {}
  • newCachedThreadPool。这种Queue的策略,对应前面所描述的第一种,Direct Handsoff。
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

核心数为0,最大线程数相当于无界,而SynchronousQueue则是一个空队列,不能存放任务,所以会直接创建线程,直到达到最大的线程数。所有闲置60s之后的线程会被回收,因此执行完之后不会占用任何资源。

  • newFixedThreadPool。这种Queue策略,对应前面所描述的第二种,Unbounded queues。
public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}

核心数等于最大线程数,因为LinkedBlockingQueue无界,所以最大线程数无实际意义。所以这种线程池保证最大线程数为nThreads,且由于keepAliveTime为0,所以不对线程进行回收;后来的任务全部扔进queue里面,等待空闲的线程来执行。

  • newSingleThreadExecutor。这种Queue策略,对应前面描述的第二种,Unbounded queues。
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>(),
                                threadFactory));
}

在FixThreadPool的基础上,保证线程数最大为1。

线程池如何工作

一般通过线程池的execute方法执行任务,在execute方法中,变浓缩了上面 内容的处理逻辑。

/*
 * Proceed in 3 steps:
 *
 * 1. If fewer than corePoolSize threads are running, try to
 * start a new thread with the given command as its first
 * task.  The call to addWorker atomically checks runState and
 * workerCount, and so prevents false alarms that would add
 * threads when it shouldn't, by returning false.
 *
 * 2. If a task can be successfully queued, then we still need
 * to double-check whether we should have added a thread
 * (because existing ones died since last checking) or that
 * the pool shut down since entry into this method. So we
 * recheck state and if necessary roll back the enqueuing if
 * stopped, or start a new thread if there are none.
 *
 * 3. If we cannot queue task, then we try to add a new
 * thread.  If it fails, we know we are shut down or saturated
 * and so reject the task.
 */

主要分三步:①与corePoolSize比较大小,小就新建线程运行任务。②大于corePoolSize,放入队列。③放入队列失败,再次尝试新建线程执行任务,上限是maximumPoolSize。④创建新线程失败,那么调用拒绝策略。

public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    int c = ctl.get();
  	// ①与corePoolSize比较大小,小就新建线程运行任务。
    if (workerCountOf(c) < corePoolSize) {
        if (addWorker(command, true))
            return;
        c = ctl.get();
    }
  	// ②大于corePoolSize,放入队列。
    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);
    }
  	// ③放入队列失败,再次尝试新建线程执行任务,上限是maximumPoolSize。
    else if (!addWorker(command, false))
      	// ④创建新线程失败,那么调用拒绝策略。
        reject(command);
}

其中比较关键的一个步骤可能是addWorker这个方法,在这其中,会调用threadFactory新建线程,并调用Thread的start()方法来开启线程。

其中有一段标号与break、continue的使用,之前都没见过、没用过。

①可以给语句块加标号赋予它们名称,标号位于语句之前,且语句前只允许加一个标号,标号后面不能跟大括号,且后面只能跟for.while.do-while等循环。

②标号只能被continue和break引用。通过用标号,我们可以对外层循环进行控制。

private boolean addWorker(Runnable firstTask, boolean core) {
    retry:
    for (int c = ctl.get();;) {
        // Check if queue empty only if necessary.
        if (runStateAtLeast(c, SHUTDOWN)
            && (runStateAtLeast(c, STOP)
                || firstTask != null
                || workQueue.isEmpty()))
            return false;

        for (;;) {
            if (workerCountOf(c)
                >= ((core ? corePoolSize : maximumPoolSize) & COUNT_MASK))
                return false;
          	// 通过CAS更新工作线程数量
            if (compareAndIncrementWorkerCount(c))
              	// break外层循环
                break retry;
            c = ctl.get();  // Re-read ctl
            if (runStateAtLeast(c, SHUTDOWN))
              	// continue外层循环
                continue retry;
            // else CAS failed due to workerCount change; retry inner loop
        }
    }

    boolean workerStarted = false;
    boolean workerAdded = false;
    Worker w = null;
    try {
      	// 调用threadFactory新建线程
        w = new Worker(firstTask);
        final Thread t = w.thread;
        if (t != null) {
            final ReentrantLock mainLock = this.mainLock;
            mainLock.lock();
            try {
                // Recheck while holding lock.
                // Back out on ThreadFactory failure or if
                // shut down before lock acquired.
                int c = ctl.get();

                if (isRunning(c) ||
                    (runStateLessThan(c, STOP) && firstTask == null)) {
                    if (t.isAlive()) // precheck that t is startable
                        throw new IllegalThreadStateException();
                    workers.add(w);
                    int s = workers.size();
                    if (s > largestPoolSize)
                        largestPoolSize = s;
                    workerAdded = true;
                }
            } finally {
                mainLock.unlock();
            }
            if (workerAdded) {
              	// 启动线程
                t.start();
                workerStarted = true;
            }
        }
    } finally {
        if (! workerStarted)
            addWorkerFailed(w);
    }
    return workerStarted;
}

Worker(Runnable firstTask) {
    setState(-1); // inhibit interrupts until runWorker
    this.firstTask = firstTask;
    this.thread = getThreadFactory().newThread(this);
}

reject即直接调用rejectedExecution方法。

final void reject(Runnable command) {
    handler.rejectedExecution(command, this);
}

线程池如何拉取队列中的任务并执行?

这里有一个细节,需要注意,Worker这个私有内部类,它实现了Runnable,并且通过threadFactory创建线程的时候,将自己作为Runnable,传给了创建的线程,而同时传进去的task被专门用firstTask保存起来。

Worker(Runnable firstTask) {
    setState(-1); // inhibit interrupts until runWorker
    this.firstTask = firstTask;
    this.thread = getThreadFactory().newThread(this);
}

所以,上面的t.start()

if (workerAdded) {
    // 启动线程
    t.start();
    workerStarted = true;
}

实际上是执行了Worker的run方法,也就是:

public void run() {
    runWorker(this);
}

final void runWorker(Worker w) {
    Thread wt = Thread.currentThread();
  	// 第一次执行时,会首先执行之前传入的task。
    Runnable task = w.firstTask;
    w.firstTask = null;
    w.unlock(); // allow interrupts
    boolean completedAbruptly = true;
    try {
      	// getTask()会堵塞线程,直到能拿到数据。
        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);
                try {
                    task.run();
                    afterExecute(task, null);
                } catch (Throwable ex) {
                    afterExecute(task, ex);
                    throw ex;
                }
            } finally {
                task = null;
                w.completedTasks++;
                w.unlock();
            }
        }
        completedAbruptly = false;
    } finally {
        processWorkerExit(w, completedAbruptly);
    }
}

其中还包含beforeExecute、afterExecute函数的执行。

Reference:

https://www.jianshu.com/p/f030aa5d7a28

https://www.cnblogs.com/dolphin0520/p/3932921.html

发布了166 篇原创文章 · 获赞 118 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/asahinokawa/article/details/104988500