I. Overview
1. The thread pool and task Invoker of the JDK are defined by Executorservice and Scheduledexecutorservice respectively, and the inheritance relationship is as follows:
/
Threadpoolexecutor: The Executorservice implementation class, whose constructors provide flexible parameter configurations, can construct multiple types of thread pools, with detailed reference to the Java advanced----threadpoolexecutor mechanism
Scheduledthreadpoolexecutor: Scheduledexecutorservice implementation class for task transfer
2. Spring task two abstractions for timed tasks:
- Taskexecutor: Same as executor in JDK, introduced to provide thread pool support for the execution of timed tasks
- TaskScheduler: Abstraction of a timed task
The inheritance relationship is as follows:
TaskScheduler needs to pass in a runnable task as a parameter and specify the time or trigger () that needs to be executed periodically Trigger
.
Spring defines the implementation class Crontrigger for the trigger interface, which supports specifying a timing policy using cron expressions, using the following:
New Crontrigger ("30 * * * *?"));
3. Cron expressions
Spring supports a cron expression of 6 parameters in the following format:
{seconds} {minute} {when} {date} {month} {week}
- Seconds: Required, the allowable range of values is 0-59, the supported special symbols include '-*/, ', ', ' indicates that a specific second will trigger the task, '-' indicates that the task will be triggered over time, ' * ' means that every second will be triggered, '/' indicates from which moment, how often each time the task is triggered.
- Points: Required, allowable value range is 0-59, support special symbol and second, meaning analogy
- When: required, the allowable range of values is 0-23, the special symbols supported are the same as the second, meaning analogy
- Date: Required, the allowable range of values is 1-31, the supported special symbol is more than the second
?
, indicating that with the {week} mutually exclusive, that is, if the {week} is explicitly specified to trigger, the {date} is meaningless, so as not to cause conflicts and confusion
- Month: Required, the allowable range of values is 1-12 (JAN-DEC), the special symbols supported are the same as the second, meaning analogy
- Week: Required, the allowable value range is 1~7 (SUN-SAT), 1 for Sunday (the first day of the week), and so on, and so on, 7 for Saturday, the supporting symbol is more than seconds
?
, the meaning of the expression is mutually exclusive with the {date}, meaning that if the {date} is explicitly specified, the {week} is meaningless.
Example:
0 0 12 * *? 0 * 14 * *? 0 0/5 14 * *? 0 10,44 3 WED
A detailed description of cron expressions can be found in the Spring timing task
Two
Spring Usage Introduction (12)--spring Task