@@ -65,6 +65,8 @@ public class ThreadPoolTaskExecutorBuilder {
65
65
66
66
private final TaskDecorator taskDecorator ;
67
67
68
+ private final Boolean virtualThreads ;
69
+
68
70
private final Set <ThreadPoolTaskExecutorCustomizer > customizers ;
69
71
70
72
public ThreadPoolTaskExecutorBuilder () {
@@ -78,13 +80,14 @@ public ThreadPoolTaskExecutorBuilder() {
78
80
this .awaitTerminationPeriod = null ;
79
81
this .threadNamePrefix = null ;
80
82
this .taskDecorator = null ;
83
+ this .virtualThreads = null ;
81
84
this .customizers = null ;
82
85
}
83
86
84
87
private ThreadPoolTaskExecutorBuilder (Integer queueCapacity , Integer corePoolSize , Integer maxPoolSize ,
85
88
Boolean allowCoreThreadTimeOut , Duration keepAlive , Boolean acceptTasksAfterContextClose ,
86
89
Boolean awaitTermination , Duration awaitTerminationPeriod , String threadNamePrefix ,
87
- TaskDecorator taskDecorator , Set <ThreadPoolTaskExecutorCustomizer > customizers ) {
90
+ TaskDecorator taskDecorator , Boolean virtualThreads , Set <ThreadPoolTaskExecutorCustomizer > customizers ) {
88
91
this .queueCapacity = queueCapacity ;
89
92
this .corePoolSize = corePoolSize ;
90
93
this .maxPoolSize = maxPoolSize ;
@@ -95,6 +98,7 @@ private ThreadPoolTaskExecutorBuilder(Integer queueCapacity, Integer corePoolSiz
95
98
this .awaitTerminationPeriod = awaitTerminationPeriod ;
96
99
this .threadNamePrefix = threadNamePrefix ;
97
100
this .taskDecorator = taskDecorator ;
101
+ this .virtualThreads = virtualThreads ;
98
102
this .customizers = customizers ;
99
103
}
100
104
@@ -107,7 +111,8 @@ private ThreadPoolTaskExecutorBuilder(Integer queueCapacity, Integer corePoolSiz
107
111
public ThreadPoolTaskExecutorBuilder queueCapacity (int queueCapacity ) {
108
112
return new ThreadPoolTaskExecutorBuilder (queueCapacity , this .corePoolSize , this .maxPoolSize ,
109
113
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
110
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .customizers );
114
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
115
+ this .customizers );
111
116
}
112
117
113
118
/**
@@ -122,7 +127,8 @@ public ThreadPoolTaskExecutorBuilder queueCapacity(int queueCapacity) {
122
127
public ThreadPoolTaskExecutorBuilder corePoolSize (int corePoolSize ) {
123
128
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , corePoolSize , this .maxPoolSize ,
124
129
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
125
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .customizers );
130
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
131
+ this .customizers );
126
132
}
127
133
128
134
/**
@@ -137,7 +143,8 @@ public ThreadPoolTaskExecutorBuilder corePoolSize(int corePoolSize) {
137
143
public ThreadPoolTaskExecutorBuilder maxPoolSize (int maxPoolSize ) {
138
144
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , maxPoolSize ,
139
145
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
140
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .customizers );
146
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
147
+ this .customizers );
141
148
}
142
149
143
150
/**
@@ -149,7 +156,8 @@ public ThreadPoolTaskExecutorBuilder maxPoolSize(int maxPoolSize) {
149
156
public ThreadPoolTaskExecutorBuilder allowCoreThreadTimeOut (boolean allowCoreThreadTimeOut ) {
150
157
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
151
158
allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
152
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .customizers );
159
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
160
+ this .customizers );
153
161
}
154
162
155
163
/**
@@ -160,7 +168,8 @@ public ThreadPoolTaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThr
160
168
public ThreadPoolTaskExecutorBuilder keepAlive (Duration keepAlive ) {
161
169
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
162
170
this .allowCoreThreadTimeOut , keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
163
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .customizers );
171
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
172
+ this .customizers );
164
173
}
165
174
166
175
/**
@@ -174,7 +183,8 @@ public ThreadPoolTaskExecutorBuilder keepAlive(Duration keepAlive) {
174
183
public ThreadPoolTaskExecutorBuilder acceptTasksAfterContextClose (boolean acceptTasksAfterContextClose ) {
175
184
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
176
185
this .allowCoreThreadTimeOut , this .keepAlive , acceptTasksAfterContextClose , this .awaitTermination ,
177
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .customizers );
186
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
187
+ this .customizers );
178
188
}
179
189
180
190
/**
@@ -188,7 +198,8 @@ public ThreadPoolTaskExecutorBuilder acceptTasksAfterContextClose(boolean accept
188
198
public ThreadPoolTaskExecutorBuilder awaitTermination (boolean awaitTermination ) {
189
199
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
190
200
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , awaitTermination ,
191
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .customizers );
201
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
202
+ this .customizers );
192
203
}
193
204
194
205
/**
@@ -203,7 +214,8 @@ public ThreadPoolTaskExecutorBuilder awaitTermination(boolean awaitTermination)
203
214
public ThreadPoolTaskExecutorBuilder awaitTerminationPeriod (Duration awaitTerminationPeriod ) {
204
215
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
205
216
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
206
- awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .customizers );
217
+ awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
218
+ this .customizers );
207
219
}
208
220
209
221
/**
@@ -214,7 +226,8 @@ public ThreadPoolTaskExecutorBuilder awaitTerminationPeriod(Duration awaitTermin
214
226
public ThreadPoolTaskExecutorBuilder threadNamePrefix (String threadNamePrefix ) {
215
227
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
216
228
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
217
- this .awaitTerminationPeriod , threadNamePrefix , this .taskDecorator , this .customizers );
229
+ this .awaitTerminationPeriod , threadNamePrefix , this .taskDecorator , this .virtualThreads ,
230
+ this .customizers );
218
231
}
219
232
220
233
/**
@@ -225,7 +238,20 @@ public ThreadPoolTaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
225
238
public ThreadPoolTaskExecutorBuilder taskDecorator (TaskDecorator taskDecorator ) {
226
239
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
227
240
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
228
- this .awaitTerminationPeriod , this .threadNamePrefix , taskDecorator , this .customizers );
241
+ this .awaitTerminationPeriod , this .threadNamePrefix , taskDecorator , this .virtualThreads ,
242
+ this .customizers );
243
+ }
244
+
245
+ /**
246
+ * Specify whether to use virtual threads instead of platform threads.
247
+ * @param virtualThreads whether to use virtual threads instead of platform threads
248
+ * @return a new builder instance
249
+ */
250
+ public ThreadPoolTaskExecutorBuilder virtualThreads (boolean virtualThreads ) {
251
+ return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
252
+ this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
253
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , virtualThreads ,
254
+ this .customizers );
229
255
}
230
256
231
257
/**
@@ -255,7 +281,8 @@ public ThreadPoolTaskExecutorBuilder customizers(Iterable<? extends ThreadPoolTa
255
281
Assert .notNull (customizers , "Customizers must not be null" );
256
282
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
257
283
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
258
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , append (null , customizers ));
284
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this .virtualThreads ,
285
+ append (null , customizers ));
259
286
}
260
287
261
288
/**
@@ -284,7 +311,7 @@ public ThreadPoolTaskExecutorBuilder additionalCustomizers(
284
311
Assert .notNull (customizers , "Customizers must not be null" );
285
312
return new ThreadPoolTaskExecutorBuilder (this .queueCapacity , this .corePoolSize , this .maxPoolSize ,
286
313
this .allowCoreThreadTimeOut , this .keepAlive , this .acceptTasksAfterContextClose , this .awaitTermination ,
287
- this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator ,
314
+ this .awaitTerminationPeriod , this .threadNamePrefix , this .taskDecorator , this . virtualThreads ,
288
315
append (this .customizers , customizers ));
289
316
}
290
317
@@ -332,6 +359,7 @@ public <T extends ThreadPoolTaskExecutor> T configure(T taskExecutor) {
332
359
map .from (this .awaitTerminationPeriod ).as (Duration ::toMillis ).to (taskExecutor ::setAwaitTerminationMillis );
333
360
map .from (this .threadNamePrefix ).whenHasText ().to (taskExecutor ::setThreadNamePrefix );
334
361
map .from (this .taskDecorator ).to (taskExecutor ::setTaskDecorator );
362
+ map .from (this .virtualThreads ).to (taskExecutor ::setVirtualThreads );
335
363
if (!CollectionUtils .isEmpty (this .customizers )) {
336
364
this .customizers .forEach ((customizer ) -> customizer .customize (taskExecutor ));
337
365
}
0 commit comments