@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2023 the original author or authors .
* Copyright 2002 - 2024 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -98,6 +98,8 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
private boolean prestartAllCoreThreads = false ;
private boolean prestartAllCoreThreads = false ;
private boolean strictEarlyShutdown = false ;
@Nullable
@Nullable
private TaskDecorator taskDecorator ;
private TaskDecorator taskDecorator ;
@ -212,7 +214,7 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
/ * *
/ * *
* Specify whether to start all core threads , causing them to idly wait for work .
* Specify whether to start all core threads , causing them to idly wait for work .
* < p > Default is "false" .
* < p > Default is "false" , starting threads and adding them to the pool on demand .
* @since 5 . 3 . 14
* @since 5 . 3 . 14
* @see java . util . concurrent . ThreadPoolExecutor # prestartAllCoreThreads
* @see java . util . concurrent . ThreadPoolExecutor # prestartAllCoreThreads
* /
* /
@ -220,6 +222,30 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
this . prestartAllCoreThreads = prestartAllCoreThreads ;
this . prestartAllCoreThreads = prestartAllCoreThreads ;
}
}
/ * *
* Specify whether to initiate an early shutdown signal on context close ,
* disposing all idle threads and rejecting further task submissions .
* < p > By default , existing tasks will be allowed to complete within the
* coordinated lifecycle stop phase in any case . This setting just controls
* whether an explicit { @link ThreadPoolExecutor # shutdown ( ) } call will be
* triggered on context close , rejecting task submissions after that point .
* < p > As of 6 . 1 . 4 , the default is "false" , leniently allowing for late tasks
* to arrive after context close , still participating in the lifecycle stop
* phase . Note that this differs from { @link # setAcceptTasksAfterContextClose }
* which completely bypasses the coordinated lifecycle stop phase , with no
* explicit waiting for the completion of existing tasks at all .
* < p > Switch this to "true" for a strict early shutdown signal analogous to
* the 6 . 1 - established default behavior of { @link ThreadPoolTaskScheduler } .
* Note that the related flags { @link # setAcceptTasksAfterContextClose } and
* { @link # setWaitForTasksToCompleteOnShutdown } will override this setting ,
* leading to a late shutdown without a coordinated lifecycle stop phase .
* @since 6 . 1 . 4
* @see # initiateShutdown ( )
* /
public void setStrictEarlyShutdown ( boolean defaultEarlyShutdown ) {
this . strictEarlyShutdown = defaultEarlyShutdown ;
}
/ * *
/ * *
* Specify a custom { @link TaskDecorator } to be applied to any { @link Runnable }
* Specify a custom { @link TaskDecorator } to be applied to any { @link Runnable }
* about to be executed .
* about to be executed .
@ -292,7 +318,7 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
/ * *
/ * *
* Create the BlockingQueue to use for the ThreadPoolExecutor .
* Create the BlockingQueue to use for the ThreadPoolExecutor .
* < p > A LinkedBlockingQueue instance will be created for a positive
* < p > A LinkedBlockingQueue instance will be created for a positive
* capacity value ; a SynchronousQueue el se.
* capacity value ; a SynchronousQueue otherwi se.
* @param queueCapacity the specified queue capacity
* @param queueCapacity the specified queue capacity
* @return the BlockingQueue instance
* @return the BlockingQueue instance
* @see java . util . concurrent . LinkedBlockingQueue
* @see java . util . concurrent . LinkedBlockingQueue
@ -424,4 +450,11 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
}
}
}
}
@Override
protected void initiateEarlyShutdown ( ) {
if ( this . strictEarlyShutdown ) {
super . initiateEarlyShutdown ( ) ;
}
}
}
}