@@ -52,6 +52,8 @@ public class SimpleAsyncTaskExecutorBuilder {
5252
5353 private final @ Nullable String threadNamePrefix ;
5454
55+ private final boolean cancelRemainingTasksOnClose ;
56+
5557 private final boolean rejectTasksWhenLimitReached ;
5658
5759 private final @ Nullable Integer concurrencyLimit ;
@@ -63,15 +65,16 @@ public class SimpleAsyncTaskExecutorBuilder {
6365 private final @ Nullable Duration taskTerminationTimeout ;
6466
6567 public SimpleAsyncTaskExecutorBuilder () {
66- this (null , null , false , null , null , null , null );
68+ this (null , null , false , false , null , null , null , null );
6769 }
6870
6971 private SimpleAsyncTaskExecutorBuilder (@ Nullable Boolean virtualThreads , @ Nullable String threadNamePrefix ,
70- boolean rejectTasksWhenLimitReached , @ Nullable Integer concurrencyLimit ,
71- @ Nullable TaskDecorator taskDecorator , @ Nullable Set < SimpleAsyncTaskExecutorCustomizer > customizers ,
72- @ Nullable Duration taskTerminationTimeout ) {
72+ boolean cancelRemainingTasksOnClose , boolean rejectTasksWhenLimitReached ,
73+ @ Nullable Integer concurrencyLimit , @ Nullable TaskDecorator taskDecorator ,
74+ @ Nullable Set < SimpleAsyncTaskExecutorCustomizer > customizers , @ Nullable Duration taskTerminationTimeout ) {
7375 this .virtualThreads = virtualThreads ;
7476 this .threadNamePrefix = threadNamePrefix ;
77+ this .cancelRemainingTasksOnClose = cancelRemainingTasksOnClose ;
7578 this .rejectTasksWhenLimitReached = rejectTasksWhenLimitReached ;
7679 this .concurrencyLimit = concurrencyLimit ;
7780 this .taskDecorator = taskDecorator ;
@@ -86,8 +89,8 @@ private SimpleAsyncTaskExecutorBuilder(@Nullable Boolean virtualThreads, @Nullab
8689 */
8790 public SimpleAsyncTaskExecutorBuilder threadNamePrefix (@ Nullable String threadNamePrefix ) {
8891 return new SimpleAsyncTaskExecutorBuilder (this .virtualThreads , threadNamePrefix ,
89- this .rejectTasksWhenLimitReached , this .concurrencyLimit , this .taskDecorator , this . customizers ,
90- this .taskTerminationTimeout );
92+ this .cancelRemainingTasksOnClose , this .rejectTasksWhenLimitReached , this .concurrencyLimit ,
93+ this .taskDecorator , this . customizers , this . taskTerminationTimeout );
9194 }
9295
9396 /**
@@ -97,8 +100,26 @@ public SimpleAsyncTaskExecutorBuilder threadNamePrefix(@Nullable String threadNa
97100 */
98101 public SimpleAsyncTaskExecutorBuilder virtualThreads (@ Nullable Boolean virtualThreads ) {
99102 return new SimpleAsyncTaskExecutorBuilder (virtualThreads , this .threadNamePrefix ,
100- this .rejectTasksWhenLimitReached , this .concurrencyLimit , this .taskDecorator , this .customizers ,
101- this .taskTerminationTimeout );
103+ this .cancelRemainingTasksOnClose , this .rejectTasksWhenLimitReached , this .concurrencyLimit ,
104+ this .taskDecorator , this .customizers , this .taskTerminationTimeout );
105+ }
106+
107+ /**
108+ * Set whether to cancel remaining tasks on close. By default {@code false} not
109+ * tracking active threads at all or just interrupting any remaining threads that
110+ * still have not finished after the specified {@link #taskTerminationTimeout
111+ * taskTerminationTimeout}. Switch this to {@code true} for immediate interruption on
112+ * close, either in combination with a subsequent termination timeout or without any
113+ * waiting at all, depending on whether a {@code taskTerminationTimeout} has been
114+ * specified as well.
115+ * @param cancelRemainingTasksOnClose whether to cancel remaining tasks on close
116+ * @return a new builder instance
117+ * @since 4.0.0
118+ */
119+ public SimpleAsyncTaskExecutorBuilder cancelRemainingTasksOnClose (boolean cancelRemainingTasksOnClose ) {
120+ return new SimpleAsyncTaskExecutorBuilder (this .virtualThreads , this .threadNamePrefix ,
121+ cancelRemainingTasksOnClose , this .rejectTasksWhenLimitReached , this .concurrencyLimit ,
122+ this .taskDecorator , this .customizers , this .taskTerminationTimeout );
102123 }
103124
104125 /**
@@ -112,8 +133,8 @@ public SimpleAsyncTaskExecutorBuilder virtualThreads(@Nullable Boolean virtualTh
112133 */
113134 public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached (boolean rejectTasksWhenLimitReached ) {
114135 return new SimpleAsyncTaskExecutorBuilder (this .virtualThreads , this .threadNamePrefix ,
115- rejectTasksWhenLimitReached , this .concurrencyLimit , this . taskDecorator , this .customizers ,
116- this .taskTerminationTimeout );
136+ this .cancelRemainingTasksOnClose , rejectTasksWhenLimitReached , this .concurrencyLimit ,
137+ this .taskDecorator , this . customizers , this . taskTerminationTimeout );
117138 }
118139
119140 /**
@@ -123,8 +144,8 @@ public SimpleAsyncTaskExecutorBuilder rejectTasksWhenLimitReached(boolean reject
123144 */
124145 public SimpleAsyncTaskExecutorBuilder concurrencyLimit (@ Nullable Integer concurrencyLimit ) {
125146 return new SimpleAsyncTaskExecutorBuilder (this .virtualThreads , this .threadNamePrefix ,
126- this .rejectTasksWhenLimitReached , concurrencyLimit , this .taskDecorator , this . customizers ,
127- this .taskTerminationTimeout );
147+ this .cancelRemainingTasksOnClose , this .rejectTasksWhenLimitReached , concurrencyLimit ,
148+ this .taskDecorator , this . customizers , this . taskTerminationTimeout );
128149 }
129150
130151 /**
@@ -134,8 +155,8 @@ public SimpleAsyncTaskExecutorBuilder concurrencyLimit(@Nullable Integer concurr
134155 */
135156 public SimpleAsyncTaskExecutorBuilder taskDecorator (@ Nullable TaskDecorator taskDecorator ) {
136157 return new SimpleAsyncTaskExecutorBuilder (this .virtualThreads , this .threadNamePrefix ,
137- this .rejectTasksWhenLimitReached , this .concurrencyLimit , taskDecorator , this .customizers ,
138- this .taskTerminationTimeout );
158+ this .cancelRemainingTasksOnClose , this .rejectTasksWhenLimitReached , this .concurrencyLimit ,
159+ taskDecorator , this . customizers , this .taskTerminationTimeout );
139160 }
140161
141162 /**
@@ -146,8 +167,8 @@ public SimpleAsyncTaskExecutorBuilder taskDecorator(@Nullable TaskDecorator task
146167 */
147168 public SimpleAsyncTaskExecutorBuilder taskTerminationTimeout (@ Nullable Duration taskTerminationTimeout ) {
148169 return new SimpleAsyncTaskExecutorBuilder (this .virtualThreads , this .threadNamePrefix ,
149- this .rejectTasksWhenLimitReached , this .concurrencyLimit , this .taskDecorator , this . customizers ,
150- taskTerminationTimeout );
170+ this .cancelRemainingTasksOnClose , this .rejectTasksWhenLimitReached , this .concurrencyLimit ,
171+ this . taskDecorator , this . customizers , taskTerminationTimeout );
151172 }
152173
153174 /**
@@ -177,8 +198,8 @@ public SimpleAsyncTaskExecutorBuilder customizers(
177198 Iterable <? extends SimpleAsyncTaskExecutorCustomizer > customizers ) {
178199 Assert .notNull (customizers , "'customizers' must not be null" );
179200 return new SimpleAsyncTaskExecutorBuilder (this .virtualThreads , this .threadNamePrefix ,
180- this .rejectTasksWhenLimitReached , this .concurrencyLimit , this .taskDecorator , append ( null , customizers ) ,
181- this .taskTerminationTimeout );
201+ this .cancelRemainingTasksOnClose , this .rejectTasksWhenLimitReached , this .concurrencyLimit ,
202+ this .taskDecorator , append ( null , customizers ), this . taskTerminationTimeout );
182203 }
183204
184205 /**
@@ -206,8 +227,8 @@ public SimpleAsyncTaskExecutorBuilder additionalCustomizers(
206227 Iterable <? extends SimpleAsyncTaskExecutorCustomizer > customizers ) {
207228 Assert .notNull (customizers , "'customizers' must not be null" );
208229 return new SimpleAsyncTaskExecutorBuilder (this .virtualThreads , this .threadNamePrefix ,
209- this .rejectTasksWhenLimitReached , this .concurrencyLimit , this .taskDecorator ,
210- append (this .customizers , customizers ), this .taskTerminationTimeout );
230+ this .cancelRemainingTasksOnClose , this .rejectTasksWhenLimitReached , this .concurrencyLimit ,
231+ this . taskDecorator , append (this .customizers , customizers ), this .taskTerminationTimeout );
211232 }
212233
213234 /**
@@ -246,6 +267,7 @@ public <T extends SimpleAsyncTaskExecutor> T configure(T taskExecutor) {
246267 PropertyMapper map = PropertyMapper .get ();
247268 map .from (this .virtualThreads ).to (taskExecutor ::setVirtualThreads );
248269 map .from (this .threadNamePrefix ).whenHasText ().to (taskExecutor ::setThreadNamePrefix );
270+ map .from (this .cancelRemainingTasksOnClose ).to (taskExecutor ::setCancelRemainingTasksOnClose );
249271 map .from (this .rejectTasksWhenLimitReached ).to (taskExecutor ::setRejectTasksWhenLimitReached );
250272 map .from (this .concurrencyLimit ).to (taskExecutor ::setConcurrencyLimit );
251273 map .from (this .taskDecorator ).to (taskExecutor ::setTaskDecorator );
0 commit comments