Home  >  Article  >  Java  >  Detailed explanation of JAVA's ThreadPoolExecutor thread pool principle and its execute method examples

Detailed explanation of JAVA's ThreadPoolExecutor thread pool principle and its execute method examples

怪我咯
怪我咯Original
2017-06-30 10:38:362590browse

The following editor will bring you an article about the ThreadPoolExecutor thread pool principle and its execute method (detailed explanation). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

##jdk1.7.0_79

Most people may use thread pools, and they also know why. It's just that tasks need to be executed asynchronously, and threads need to be managed uniformly. Regarding obtaining threads from the thread pool, most people may only know that if I need a thread to perform a task, then I will throw the task into the thread pool. If there are idle threads in the thread pool, they will be executed. If there are no idle threads, they will be executed. Just wait. In fact, the execution principle of the thread pool is far more than that simple.

The thread pool class - ThreadPoolExecutor is provided in the Java concurrency package. In fact, more of us may use the thread pool provided by the Executors factory class: newFixedThreadPool, newSingleThreadPool, newCachedThreadPool, these three The thread pool is not a subclass of ThreadPoolExecutor. Regarding the relationship between these, let's first look at ThreadPoolExecutor. Check the source code and find that it has a total of 4

construction methods.

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

First, let’s start with these parameters to understand the execution principle of the thread pool ThreadPoolExecutor.

corePoolSize:The number of threads in the core thread pool

maximumPoolSize:Maximum number of thread pool threads

##keepAliveTime: Thread activity retention time, after the worker thread of the thread pool is idle, Stay alive for as long as you need.

unit: Unit of thread activity retention time.

workQueue: Specify the blocking queue used by the task queue corePoolSize and maximumPoolSize are both in the specified thread The number of threads in the pool, it seems that when you usually use the thread pool, you only need to pass a parameter of the thread pool size to create a thread pool. Java provides us with some commonly used thread pool classes, namely the newFixedThreadPool mentioned above. , newSingleThreadExecutor, newCachedThreadPool. Of course, if we want to create a custom thread pool ourselves, we have to "configure" some parameters related to the thread pool ourselves.

When a task is handed over to the thread pool for processing, the execution principle of the thread pool is as shown in the figure below. Refer to "The Art of Java Concurrent Programming"

① First, it will be judged whether there are threads in the core thread pool that can be executed. If there are idle threads, a thread will be created to perform the task.

②When there are no threads available for execution in the core thread pool, the task is thrown into the task queue.

③If the task queue (bounded) is also full, but the number of running threads is less than the maximum number of thread pools, a new thread will be created to execute the task, but if the running When the number of threads has reached the maximum number of thread pools, threads cannot be created to perform tasks.

So in fact, the thread pool does not just simply throw the task into the thread pool. If there are threads in the thread pool, the task will be executed, and if there are no threads, it will wait.

To consolidate the principles of thread pools, let’s now learn about the three commonly used thread pools mentioned above:

Executors.newFixedThreadPool: Create a thread pool with a fixed number of threads.

// Executors#newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads) {
 return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}
You can see that the ThreadPoolExecutor class is called in newFixedThreadPool, and the passed parameter corePoolSize= maximumPoolSize=nThread. Looking back at the execution principle of the thread pool, when a task is submitted to the thread pool, it is first determined whether there are idle threads in the core thread pool. If there are idle threads, a thread will be created. If not, the task will be placed in the task queue (here is the bounded blocking queue LinkedBlockingQueue). , if the task queue is full, for newFixedThreadPool, its maximum number of thread pools = number of core thread pools. At this time, the task queue is also full, and new threads cannot be expanded to execute tasks.

Executors.newSingleThreadExecutor: Create a thread pool containing only one thread.

 

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

只有一个线程的线程池好像有点奇怪,并且并没有直接将返回ThreadPoolExecutor,甚至也没有直接将线程池数量1传递给newFixedThreadPool返回。那就说明这个只含有一个线程的线程池,或许并没有只包含一个线程那么简单。在其源码注释中这么写到:创建只有一个工作线程的线程池用于操作一个无界队列(如果由于前驱节点的执行被终止结束了,一个新的线程将会继续执行后继节点线程)任务得以继续执行,不同于newFixedThreadPool(1)不会有额外的线程来重新继续执行后继节点。也就是说newSingleThreadExecutor自始至终都只有一个线程在执行,这和newFixedThreadPool一样,但如果线程终止结束过后newSingleThreadExecutor则会重新创建一个新的线程来继续执行任务队列中的线程,而newFixedThreaPool则不会。

Executors.newCachedThreadPool:根据需要创建新线程的线程池。

//Executors#newCachedThreadPool
public static ExecutorService newCachedThreadPool() {
  return new ThreadPooExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}

可以看到newCachedThread返回的是ThreadPoolExecutor,其参数核心线程池corePoolSize = 0, maximumPoolSize = Integer.MAX_VALUE,这也就是说当任务被提交到newCachedThread线程池时,将会直接把任务放到SynchronousQueue任务队列中,maximumPool从任务队列中获取任务。注意SynchronousQueue是一个没有容量的队列,也就是说每个入队操作必须等待另一个线程的对应出队操作,如果主线程提交任务的速度高于maximumPool中线程处理任务的速度时,newCachedThreadPool会不断创建线程,线程多并不是一件好事,严重会耗尽CPU和内存资源。

题外话:newFixedThreadPool、newSingleThreadExecutor、newCachedThreadPool,这三者都直接或间接调用了ThreadPoolExecutor,为什么它们三者没有直接是其子类,而是通过Executors来实例化呢?这是所采用的静态工厂方法,在java.util.Connections接口中同样也是采用的静态工厂方法来创建相关的类。这样有很多好处,静态工厂方法是用来产生对象的,产生什么对象没关系,只要返回原返回类型或原返回类型的子类型都可以,降低API数目和使用难度,在《Effective Java》中的第1条就是静态工厂方法。

回到ThreadPoolExecutor,首先来看它的继承关系:

ThreadPoolExecutor它的顶级父类是Executor接口,只包含了一个方法——execute,这个方法也就是线程池的“执行”。

//Executor#execute
public interface Executor {
 void execute(Runnable command);
}

Executor#execute的实现则是在ThreadPoolExecutor中实现的:

//ThreadPoolExecutor#execute
public void execute(Runnable command) {
  if (command == null) 
  throw new NullPointerException();
  int c = ctl.get(); 
 …
}

一来就碰到个不知所云的ctl变量它的定义:

private final AtomicInteger ctl = new AtlmicInteger(ctlOf(RUNNING, 0));

这个变量使用来干嘛的呢?它的作用有点类似我们在《ReadWriteLock接口及其实现ReentrantReadWriteLock》中提到的读写锁有读、写两个同步状态,而AQS则只提供了state一个int型变量,此时将state高16位表示为读状态,低16位表示为写状态。这里的clt同样也是,它表示了两个概念:

workerCount:当前有效的线程数

runState:当前线程池的五种状态,Running、Shutdown、Stop、Tidying、Terminate。

int型变量一共有32位,线程池的五种状态runState至少需要3位来表示,故workCount只能有29位,所以代码中规定线程池的有效线程数最多为229-1。

//ThreadPoolExecutor
private static final int COUNT_BITS = Integer.SIZE – 3;  //32-3=29,线程数量所占位数
private static final int CAPACITY = (1 << COUNT_BITS) – 1; //低29位表示最大线程数,229-1
//五种线程池状态
private static final int RUNNING = -1 << COUNT_BITS; /int型变量高3位(含符号位)101表RUNING
private static final int SHUTDOWN = 0 << COUNT_BITS; //高3位000
private static final int STOP = 1 << COUNT_BITS; //高3位001
private static final int TIDYING = 2 << COUNT_BITS; //高3位010
private static final int TERMINATED = 3 << COUNT_BITS; //高3位011

再次回到ThreadPoolExecutor#execute方法:

//ThreadPoolExecutor#execute
public void execute(Runnable command) {
 if (command == null) 
  throw new NullPointerException();
   int c = ctl.get(); //由它可以获取到当前有效的线程数和线程池的状态
/*1.获取当前正在运行线程数是否小于核心线程池,是则新创建一个线程执行任务,否则将任务放到任务队列中*/
 if (workerCountOf(c) < corePoolSize){
  if (addWorker(command, tre))  //在addWorker中创建工作线程执行任务
   return ;
  c = ctl.get();
 }
/*2.当前核心线程池中全部线程都在运行workerCountOf(c) >= corePoolSize,所以此时将线程放到任务队列中*/
 if (isRunning(c) && workQueue.offer(command)) { //线程池是否处于运行状态,且是否任务插入任务队列成功
  int recheck = ctl.get();
     if (!isRunning(recheck) && remove(command))  //线程池是否处于运行状态,如果不是则使刚刚的任务出队
       reject(command); //抛出RejectedExceptionException异常
     else if (workerCountOf(recheck) == 0)
       addWorker(null, false);
  }
/*3.插入队列不成功,且当前线程数数量小于最大线程池数量,此时则创建新线程执行任务,创建失败抛出异常*/
  else if (!addWorker(command, false)){
    reject(command); //抛出RejectedExceptionException异常
  }
}

上面代码注释第7行的即判断当前核心线程池里是否有空闲线程,有则通过addWorker方法创建工作线程执行任务。addWorker方法较长,筛选出重要的代码来解析。

//ThreadPoolExecutor#addWorker
private boolean addWorker(Runnable firstTask, boolean core) {
/*首先会再次检查线程池是否处于运行状态,核心线程池中是否还有空闲线程,都满足条件过后则会调用compareAndIncrementWorkerCount先将正在运行的线程数+1,数量自增成功则跳出循环,自增失败则继续从头继续循环*/
  ...
  if (compareAndIncrementWorkerCount(c))
    break retry;
  ...
/*正在运行的线程数自增成功后则将线程封装成工作线程Worker*/
  boolean workerStarted = false;
  boolean workerAdded = false;
  Worker w = null;
  try {
    final ReentrantLock mainLock = this.mainLock;  //全局锁
    w = new Woker(firstTask);  //将线程封装为Worker工作线程
    final Thread t = w.thread;
    if (t != null) {
      mainLock.lock(); //获取全局锁
/*当持有了全局锁的时候,还需要再次检查线程池的运行状态等*/
      try {
        int c = clt.get();
        int rs = runStateOf(c);  //线程池运行状态
        if (rs < SHUTDOWN || (rs == SHUTDOWN && firstTask == null)){  //线程池处于运行状态,或者线程池关闭且任务线程为空
          if (t.isAlive()) //线程处于活跃状态,即线程已经开始执行或者还未死亡,正确的应线程在这里应该是还未开始执行的
            throw new IllegalThreadStateException();
          workers.add(w); //private final HashSet<Worker> wokers = new HashSet<Worker>();包含线程池中所有的工作线程,只有在获取了全局的时候才能访问它。将新构造的工作线程加入到工作线程集合中
          int s = worker.size(); //工作线程数量
          if (s > largestPoolSize)
            largestPoolSize = s;
          workerAdded = true; //新构造的工作线程加入成功
        }
      } finally {
        mainLock.unlock();
      }
      if (workerAdded) {
        t.start(); //在被构造为Worker工作线程,且被加入到工作线程集合中后,执行线程任务,注意这里的start实际上执行Worker中run方法,所以接下来分析Worker的run方法
        workerStarted = true;
      }
    }
  } finally {
    if (!workerStarted) //未能成功创建执行工作线程
      addWorkerFailed(w); //在启动工作线程失败后,将工作线程从集合中移除
  }
  return workerStarted;
}

在上面第35代码中,工作线程被成功添加到工作线程集合中后,则开始start执行,这里start执行的是Worker工作线程中的run方法。

//ThreadPoolExecutor$Worker,它继承了AQS,同时实现了Runnable,所以它具备了这两者的所有特性
private final class Worker extends AbstractQueuedSynchronizer implements Runnable {
  final Thread thread;
  Runnable firstTask;
  public Worker(Runnable firstTask) {
    setState(-1); //设置AQS的同步状态为-1,禁止中断,直到调用runWorker
    this.firstTask = firstTask;
    this.thread = getThreadFactory().newThread(this); //通过线程工厂来创建一个线程,将自身作为Runnable传递传递
  }
  public void run() {
    runWorker(this); //运行工作线程
  }
}

ThreadPoolExecutor#runWorker,在此方法中,Worker在执行完任务后,还会循环获取任务队列里的任务执行(其中的getTask方法),也就是说Worker不仅仅是在执行完给它的任务就释放或者结束,它不会闲着,而是继续从任务队列中获取任务,直到任务队列中没有任务可执行时,它才退出循环完成任务。理解了以上的源码过后,往后线程池执行原理的第二步、第三步的理解实则水到渠成。

The above is the detailed content of Detailed explanation of JAVA's ThreadPoolExecutor thread pool principle and its execute method examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn