ThreadPoolExecutor的corePoolSize、maximumPoolSize和poolSize

看两段源码:

  1 public ThreadPoolExecutor(int corePoolSize,
  2 
  3         int maximumPoolSize,
  4 
  5         long keepAliveTime,
  6 
  7         TimeUnit unit,
  8 
  9         BlockingQueue<Runnable> workQueue,
 10 
 11         ThreadFactory threadFactory,
 12 
 13         RejectedExecutionHandler handler) {
 14 
 15         if (corePoolSize < 0 ||
 16 
 17         maximumPoolSize <= 0 ||
 18 
 19         maximumPoolSize < corePoolSize ||
 20 
 21         keepAliveTime < 0)
 22 
 23         throw new IllegalArgumentException();
 24 
 25         if (workQueue == null || threadFactory == null || handler == null)
 26 
 27         throw new NullPointerException();
 28 
 29         this.acc = System.getSecurityManager() == null ?
 30 
 31         null :
 32 
 33         AccessController.getContext();
 34 
 35         this.corePoolSize = corePoolSize;
 36 
 37         this.maximumPoolSize = maximumPoolSize;
 38 
 39         this.workQueue = workQueue;
 40 
 41         this.keepAliveTime = unit.toNanos(keepAliveTime);
 42 
 43         this.threadFactory = threadFactory;
 44 
 45         this.handler = handler;
 46 
 47         }
 48 
 49 private boolean addWorker(Runnable firstTask, boolean core) {
 50 
 51         retry:
 52 
 53         for (;;) {
 54 
 55         int c = ctl.get();
 56 
 57         int rs = runStateOf(c);
 58 
 59     // Check if queue empty only if necessary.
 60 
 61         if (rs >= SHUTDOWN &&
 62 
 63         ! (rs == SHUTDOWN &&
 64 
 65         firstTask == null &&
 66 
 67         ! workQueue.isEmpty()))
 68 
 69         return false;
 70 
 71         for (;;) {
 72 
 73         int wc = workerCountOf(c);
 74 
 75         if (wc >= CAPACITY ||
 76 
 77         wc >= (core ? corePoolSize : maximumPoolSize))
 78 
 79         return false;
 80 
 81         if (compareAndIncrementWorkerCount(c))
 82 
 83         break retry;
 84 
 85         c = ctl.get(); // Re-read ctl
 86 
 87         if (runStateOf(c) != rs)
 88 
 89         continue retry;
 90 
 91     // else CAS failed due to workerCount change; retry inner loop
 92 
 93         }
 94 
 95         }
 96 
 97         boolean workerStarted = false;
 98 
 99         boolean workerAdded = false;
100 
101         Worker w = null;
102 
103         try {
104 
105         w = new Worker(firstTask);
106 
107     final Thread t = w.thread;
108 
109         if (t != null) {
110 
111     final ReentrantLock mainLock = this.mainLock;
112 
113         mainLock.lock();
114 
115         try {
116 
117 // Recheck while holding lock.
118 
119 // Back out on ThreadFactory failure or if
120 
121 // shut down before lock acquired.
122 
123         int rs = runStateOf(ctl.get());
124 
125         if (rs < SHUTDOWN ||
126 
127         (rs == SHUTDOWN && firstTask == null)) {
128 
129         if (t.isAlive()) // precheck that t is startable
130 
131         throw new IllegalThreadStateException();
132 
133         workers.add(w);
134 
135         int s = workers.size();
136 
137         if (s > largestPoolSize)
138 
139         largestPoolSize = s;
140 
141         workerAdded = true;
142 
143         }
144 
145         } finally {
146 
147         mainLock.unlock();
148 
149         }
150 
151         if (workerAdded) {
152 
153         t.start();
154 
155         workerStarted = true;
156 
157         }
158 
159         }
160 
161         } finally {
162 
163         if (! workerStarted)
164 
165         addWorkerFailed(w);
166 
167         }
168 
169         return workerStarted;
170 
171         }

以上可以看出,ThreadPoolExecutor的主要参数有:corePoolSize , maximumPoolSize , keepAliveTime ,workQueue,threadFactory,handler ,对于几个参数,我们该如何理解呢?

先看看这几个参数:

corePoolSize :

顾名思义,核心线程大小,即在没有任务需要执行的时候线程池的大小,并且只有在工作队列满了的情况下才会创建超出这个数量的线程;

maximuxPoolSize :

线程池中允许创建的最大线程数大小;

poolSize :

当前线程池中线程的数量,当改值为0的时候,意味着没有任何线程,线程池会终止;同时,poolSize不会超过maximumPoolSize;

看看官方解释:

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.

理解:

1、如果当前线程池的线程数还没有达到核心线程数大小,即 poolSize < corePoolSize ,无论是否有空闲的线程,系统回新增一个线程来处理新提交的任务;

2、如果当前线程池的线程数大于或者等于核心线程数大小,即 poolSize >= corePoolSize,且任务队列未满时,将提交的任务提交到阻塞队列中,等待处理workQueue.offer(command);

3、如果当前线程池的线程数大于或者等于核心线程数大小,即 poolSize >= corePoolSize,且任务队列已满时,分以下两种情况:

3.1、poolSize < maximumPoolSize ,新增线程来处理任务;

3.2、poolSize = maximuxPoolSize ,意味中线程池的处理能力已经达到极限,此时会拒绝增加新的任务,至于如何拒绝,取决于RejectedExecutionHandler

原文地址:https://www.cnblogs.com/pretttyboy/p/11325876.html