Java Concurrency --ThreadPoolExecutor Comments

Detailed ThreadPoolExecutor

Core class is the ThreadPoolExecutor Executor frame, it is implemented thread pool class, mainly consists of the following four components.

  • corePool : the size of the core thread pool.
  • maximumPool : The maximum size of the thread pool.
  • BlockingQueue : used to temporarily save the task of the work queue.
  • RejectedExecutionHandler : When ThreadPoolExecutor closed or ThreadPoolExecutor already saturated (reached the maximum thread pool size and the work queue is full), execute () method will be called Handler.

Executors Executor framework by tools, you can create three types of ThreadPoolExecutor:

  • FixedThreadPool。
  • SingleThreadExecutor。
  • CachedThreadPool。

FixedThreadPool

FixedThreadPool referred reusable fixed number of threads the thread pool. Below is the source code that implements FixedThreadPool.
Here Insert Picture Description
FixedThreadPool of corePoolSize and maximumPoolSize are specified for the parameters when creating FixedThreadPool nThreads.

When the number of threads in the thread pool greater than corePoolSize, keepAliveTime as excess idle threads waiting for new tasks longest time, excess threads will be terminated after more than this time. Here the keepAliveTime set to 0L, means that excess idle threads will be terminated immediately.

FixedThreadPool operation of execute () method of the schematic shown below:

Here Insert Picture Description

  1. If the number of threads that are currently running less than corePoolSize, creating a new thread to execute the task.
  2. 在线程池完成预热之后(当前运行的线程数等于corePoolSize),将任务加入LinkedBlockingQueue。
  3. 线程执行完1中的任务后,会在循环中反复从LinkedBlockingQueue获取任务来执行。

FixedThreadPool使用无界队列LinkedBlockingQueue作为线程池的工作队列(队列的容量为Integer.MAX_VALUE)。

使用无界队列作为工作队列会对线程池带来如下影响:

  1. 当线程池中的线程数达到corePoolSize后,新任务将在无界队列中等待,因此线程池中的线程数不会超过corePoolSize。
  2. 由于1,使用无界队列时maximumPoolSize将是一个无效参数。
  3. 由于1和2,使用无界队列时keepAliveTime将是一个无效参数。
  4. 由于使用无界队列,运行中的FixedThreadPool(未执行方法shutdown()或shutdownNow())不会拒绝任务(不会调用RejectedExecutionHandler.rejectedExecution方法)。

SingleThreadExecutor

SingleThreadExecutor是使用单个worker线程的Executor。下面是SingleThreadExecutor的源代码实现。
Here Insert Picture Description
SingleThreadExecutor的corePoolSize和maximumPoolSize被设置为1。其他参数与FixedThreadPool相同。SingleThreadExecutor使用无界队列LinkedBlockingQueue作为线程池的工作队列(队列的容量为Integer.MAX_VALUE)。SingleThreadExecutor使用无界队列作为工作队列对线程池带来的影响与FixedThreadPool相同,这里就不赘述了。

SingleThreadExecutor的运行示意图如下图所示:
Here Insert Picture Description

  1. 如果当前运行的线程数少于corePoolSize(即线程池中无运行的线程),则创建一个新线程来执行任务。
  2. 在线程池完成预热之后(当前线程池中有一个运行的线程),将任务加入LinkedBlockingQueue。
  3. 线程执行完1中的任务后,会在一个无限循环中反复从LinkedBlockingQueue获取任务来执行。

CachedThreadPool

CachedThreadPool是一个会根据需要创建新线程的线程池。下面是创建CachedThreadPool的源码。
Here Insert Picture Description
CachedThreadPool的corePoolSize被设置为0,即corePool为空;maximumPoolSize被设置为Integer.MAX_VALUE,即maximumPool是无界的。这里把keepAliveTime设置为60L,意味着CachedThreadPool中的空闲线程等待新任务的最长时间为60秒,空闲线程超过60秒后将会被终止。

FixedThreadPool和SingleThreadExecutor使用无界队列LinkedBlockingQueue作为线程池的工作队列。 CachedThreadPool使用没有容量的SynchronousQueue作为线程池的工作队列,但CachedThreadPool的maximumPool是无界的。这意味着,如果主线程提交任务的速度高于maximumPool中线程处理任务的速度时,CachedThreadPool会不断创建新线程。极端情况下,CachedThreadPool会因为创建过多线程而耗尽CPU和内存资源。

CachedThreadPool的execute()方法的执行示意图如下图所示:
Here Insert Picture Description

  1. 首先执行SynchronousQueue.offer(Runnable task)。如果当前maximumPool中有空闲线程正在执行SynchronousQueue.poll(keepAliveTime,TimeUnit.NANOSECONDS),那么主线程执行offer操作与空闲线程执行的poll操作配对成功,主线程把任务交给空闲线程执行,execute()方法执行完成;否则执行下面的步骤2)。

  2. 当初始maximumPool为空,或者maximumPool中当前没有空闲线程时,将没有线程执行SynchronousQueue.poll(keepAliveTime,TimeUnit.NANOSECONDS)。这种情况下,步骤1)失败。此时CachedThreadPool会创建一个新线程执行任务,execute()方法执行完成。

  3. 在步骤2)中新创建的线程将任务执行完后,会执行SynchronousQueue.poll(keepAliveTime,TimeUnit.NANOSECONDS)。这个poll操作会让空闲线程最多在SynchronousQueue中等待60秒钟。如果60秒钟内主线程提交了一个新任务(主线程执行步骤1)),那么这个空闲线程将执行主线程提交的新任务;否则,这个空闲线程将终止。由于空闲60秒的空闲线程会被终止,因此长时间保持空闲的CachedThreadPool不会使用任何资源。

SynchronousQueue是一个没有容量的阻塞队列。 每个插入操作必须等待另一个线程的对应移除操作,反之亦然。CachedThreadPool使用SynchronousQueue,把主线程提交的任务传递给空闲线程执行。

CachedThreadPool中任务传递的示意图如下图所示:
Here Insert Picture Description

Published 660 original articles · won praise 1889 · Views 240,000 +

Guess you like

Origin blog.csdn.net/cold___play/article/details/104070677