|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -98,6 +98,8 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
|
98 | 98 |
|
99 | 99 | private boolean prestartAllCoreThreads = false;
|
100 | 100 |
|
| 101 | + private boolean strictEarlyShutdown = false; |
| 102 | + |
101 | 103 | @Nullable
|
102 | 104 | private TaskDecorator taskDecorator;
|
103 | 105 |
|
@@ -212,14 +214,38 @@ public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
|
212 | 214 |
|
213 | 215 | /**
|
214 | 216 | * Specify whether to start all core threads, causing them to idly wait for work.
|
215 |
| - * <p>Default is "false". |
| 217 | + * <p>Default is "false", starting threads and adding them to the pool on demand. |
216 | 218 | * @since 5.3.14
|
217 | 219 | * @see java.util.concurrent.ThreadPoolExecutor#prestartAllCoreThreads
|
218 | 220 | */
|
219 | 221 | public void setPrestartAllCoreThreads(boolean prestartAllCoreThreads) {
|
220 | 222 | this.prestartAllCoreThreads = prestartAllCoreThreads;
|
221 | 223 | }
|
222 | 224 |
|
| 225 | + /** |
| 226 | + * Specify whether to initiate an early shutdown signal on context close, |
| 227 | + * disposing all idle threads and rejecting further task submissions. |
| 228 | + * <p>By default, existing tasks will be allowed to complete within the |
| 229 | + * coordinated lifecycle stop phase in any case. This setting just controls |
| 230 | + * whether an explicit {@link ThreadPoolExecutor#shutdown()} call will be |
| 231 | + * triggered on context close, rejecting task submissions after that point. |
| 232 | + * <p>As of 6.1.4, the default is "false", leniently allowing for late tasks |
| 233 | + * to arrive after context close, still participating in the lifecycle stop |
| 234 | + * phase. Note that this differs from {@link #setAcceptTasksAfterContextClose} |
| 235 | + * which completely bypasses the coordinated lifecycle stop phase, with no |
| 236 | + * explicit waiting for the completion of existing tasks at all. |
| 237 | + * <p>Switch this to "true" for a strict early shutdown signal analogous to |
| 238 | + * the 6.1-established default behavior of {@link ThreadPoolTaskScheduler}. |
| 239 | + * Note that the related flags {@link #setAcceptTasksAfterContextClose} and |
| 240 | + * {@link #setWaitForTasksToCompleteOnShutdown} will override this setting, |
| 241 | + * leading to a late shutdown without a coordinated lifecycle stop phase. |
| 242 | + * @since 6.1.4 |
| 243 | + * @see #initiateShutdown() |
| 244 | + */ |
| 245 | + public void setStrictEarlyShutdown(boolean defaultEarlyShutdown) { |
| 246 | + this.strictEarlyShutdown = defaultEarlyShutdown; |
| 247 | + } |
| 248 | + |
223 | 249 | /**
|
224 | 250 | * Specify a custom {@link TaskDecorator} to be applied to any {@link Runnable}
|
225 | 251 | * about to be executed.
|
@@ -292,7 +318,7 @@ protected void afterExecute(Runnable task, Throwable ex) {
|
292 | 318 | /**
|
293 | 319 | * Create the BlockingQueue to use for the ThreadPoolExecutor.
|
294 | 320 | * <p>A LinkedBlockingQueue instance will be created for a positive
|
295 |
| - * capacity value; a SynchronousQueue else. |
| 321 | + * capacity value; a SynchronousQueue otherwise. |
296 | 322 | * @param queueCapacity the specified queue capacity
|
297 | 323 | * @return the BlockingQueue instance
|
298 | 324 | * @see java.util.concurrent.LinkedBlockingQueue
|
@@ -424,4 +450,11 @@ protected void cancelRemainingTask(Runnable task) {
|
424 | 450 | }
|
425 | 451 | }
|
426 | 452 |
|
| 453 | + @Override |
| 454 | + protected void initiateEarlyShutdown() { |
| 455 | + if (this.strictEarlyShutdown) { |
| 456 | + super.initiateEarlyShutdown(); |
| 457 | + } |
| 458 | + } |
| 459 | + |
427 | 460 | }
|
0 commit comments