21 |
|
* statistics, such as the number of completed tasks. |
22 |
|
* |
23 |
|
* <p>To be useful across a wide range of contexts, this class |
24 |
< |
* provides many adjustable parameters and extensibility hooks. For |
25 |
< |
* example, it can be configured to create a new thread for each task, |
26 |
< |
* or even to execute tasks sequentially in a single thread, in |
27 |
< |
* addition to its most common configuration, which reuses a pool of |
28 |
< |
* threads. However, programmers are urged to use the more convenient |
24 |
> |
* provides many adjustable parameters and extensibility |
25 |
> |
* hooks. However, programmers are urged to use the more convenient |
26 |
|
* {@link Executors} factory methods {@link |
27 |
|
* Executors#newCachedThreadPool} (unbounded thread pool, with |
28 |
|
* automatic thread reclamation), {@link Executors#newFixedThreadPool} |
30 |
|
* Executors#newSingleThreadExecutor} (single background thread), that |
31 |
|
* preconfigure settings for the most common usage |
32 |
|
* scenarios. Otherwise, use the following guide when manually |
33 |
< |
* configuring and tuning <tt>ThreadPoolExecutor</tt>: |
33 |
> |
* configuring and tuning this class: |
34 |
|
* |
35 |
|
* <dl> |
36 |
|
* |
121 |
|
* new thread will be constructed. This policy avoids lockups when |
122 |
|
* handling sets of requests that might have internal dependencies. |
123 |
|
* Direct handoffs generally require unbounded maximumPoolSizes to |
124 |
< |
* avoid rejection of new submitted tasks, which in turn admits the |
124 |
> |
* avoid rejection of new submitted tasks. This in turn admits the |
125 |
|
* possibility of unbounded thread growth when commands continue to |
126 |
|
* arrive on average faster than they can be processed. </li> |
127 |
|
* |
147 |
|
* lead to artifically low throughput. If tasks frequently block (for |
148 |
|
* example if they are I/O bound), a system may be able to schedule |
149 |
|
* time for more threads than you otherwise allow. Use of small queues |
150 |
< |
* or queueless handoffs generally requires larger pool sizes, which |
151 |
< |
* keeps CPUs busier but may encounter unacceptable scheduling |
152 |
< |
* overhead, which also decreases throughput. </li> |
150 |
> |
* generally requires larger pool sizes, which keeps CPUs busier but |
151 |
> |
* may encounter unacceptable scheduling overhead, which also |
152 |
> |
* decreases throughput. </li> |
153 |
|
* |
154 |
|
* </ol> |
155 |
|
* |
208 |
|
* |
209 |
|
* <dt>Queue maintenance</dt> |
210 |
|
* |
211 |
< |
* <dd> Method {@link ThreadPoolExecutor#getQueue} allows access |
212 |
< |
* to the work queue for purposes of monitoring and debugging. |
213 |
< |
* Use of this method for any other purpose is strongly discouraged. |
214 |
< |
* Two supplied methods, {@link ThreadPoolExecutor#remove} and |
215 |
< |
* {@link ThreadPoolExecutor#purge} are available to assist in |
216 |
< |
* storage reclamation when large numbers of not-yet-executed |
217 |
< |
* tasks become cancelled.</dd> |
221 |
< |
* </dl> |
211 |
> |
* <dd> Method {@link ThreadPoolExecutor#getQueue} allows access to |
212 |
> |
* the work queue for purposes of monitoring and debugging. Use of |
213 |
> |
* this method for any other purpose is strongly discouraged. Two |
214 |
> |
* supplied methods, {@link ThreadPoolExecutor#remove} and {@link |
215 |
> |
* ThreadPoolExecutor#purge} are available to assist in storage |
216 |
> |
* reclamation when large numbers of queued tasks become |
217 |
> |
* cancelled.</dd> </dl> |
218 |
|
* |
219 |
|
* @since 1.5 |
220 |
|
* @author Doug Lea |
1232 |
|
* <tt>super.afterExecute</tt> at the beginning of this method. |
1233 |
|
* |
1234 |
|
* @param r the runnable that has completed. |
1235 |
< |
* @param t the exception that cause termination, or null if |
1235 |
> |
* @param t the exception that caused termination, or null if |
1236 |
|
* execution completed normally. |
1237 |
|
*/ |
1238 |
|
protected void afterExecute(Runnable r, Throwable t) { } |
1254 |
|
public static class CallerRunsPolicy implements RejectedExecutionHandler { |
1255 |
|
|
1256 |
|
/** |
1257 |
< |
* Constructs a <tt>CallerRunsPolicy</tt>. |
1257 |
> |
* Creates a <tt>CallerRunsPolicy</tt>. |
1258 |
|
*/ |
1259 |
|
public CallerRunsPolicy() { } |
1260 |
|
|
1261 |
+ |
/** |
1262 |
+ |
* Executes task r in the caller's thread, unless the executor |
1263 |
+ |
* has been shut down, in which case the task is discarded. |
1264 |
+ |
* @param r the runnable task requested to be executed |
1265 |
+ |
* @param e the executor attempting to execute this task |
1266 |
+ |
*/ |
1267 |
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { |
1268 |
|
if (!e.isShutdown()) { |
1269 |
|
r.run(); |
1278 |
|
public static class AbortPolicy implements RejectedExecutionHandler { |
1279 |
|
|
1280 |
|
/** |
1281 |
< |
* Constructs a <tt>AbortPolicy</tt>. |
1281 |
> |
* Creates a <tt>AbortPolicy</tt>. |
1282 |
|
*/ |
1283 |
|
public AbortPolicy() { } |
1284 |
|
|
1285 |
+ |
/** |
1286 |
+ |
* Always throws RejectedExecutionException |
1287 |
+ |
* @param r the runnable task requested to be executed |
1288 |
+ |
* @param e the executor attempting to execute this task |
1289 |
+ |
* @throws RejectedExecutionException always. |
1290 |
+ |
*/ |
1291 |
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { |
1292 |
|
throw new RejectedExecutionException(); |
1293 |
|
} |
1300 |
|
public static class DiscardPolicy implements RejectedExecutionHandler { |
1301 |
|
|
1302 |
|
/** |
1303 |
< |
* Constructs <tt>DiscardPolicy</tt>. |
1303 |
> |
* Creates <tt>DiscardPolicy</tt>. |
1304 |
|
*/ |
1305 |
|
public DiscardPolicy() { } |
1306 |
|
|
1307 |
+ |
/** |
1308 |
+ |
* Does nothing, which has the effect of discarding task r. |
1309 |
+ |
* @param r the runnable task requested to be executed |
1310 |
+ |
* @param e the executor attempting to execute this task |
1311 |
+ |
*/ |
1312 |
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { |
1313 |
|
} |
1314 |
|
} |
1320 |
|
*/ |
1321 |
|
public static class DiscardOldestPolicy implements RejectedExecutionHandler { |
1322 |
|
/** |
1323 |
< |
* Constructs a <tt>DiscardOldestPolicy</tt> for the given executor. |
1323 |
> |
* Creates a <tt>DiscardOldestPolicy</tt> for the given executor. |
1324 |
|
*/ |
1325 |
|
public DiscardOldestPolicy() { } |
1326 |
|
|
1327 |
+ |
/** |
1328 |
+ |
* Obtains and ignores the next task that the executor |
1329 |
+ |
* would otherwise execute, if one is immediately available, |
1330 |
+ |
* and then retries execution of task r, unless the executor |
1331 |
+ |
* is shut down, in which case task r is instead discarded. |
1332 |
+ |
* @param r the runnable task requested to be executed |
1333 |
+ |
* @param e the executor attempting to execute this task |
1334 |
+ |
*/ |
1335 |
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { |
1336 |
|
if (!e.isShutdown()) { |
1337 |
|
e.getQueue().poll(); |