当前位置:网站首页>When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
2022-07-07 03:12:00 【Liu Shuijing】
Spring Task There is no doubt that is Spring The first choice for single machine scheduled tasks in the environment . It's very easy to use , The function is also enough .
Spring Task There are three patterns , Namely :fixedDelay、cron and fixedRate. Don't talk much , Let's look at the code first :
@Slf4j
@Component
public class TimeTask {
private int[] people = {
6, 2, 3, 1};
private int count = 0;
@Scheduled(fixedDelay = 5000)
public void fixedDelayTask() throws InterruptedException {
if (count < 4) {
int timeConsuming = people[count];
log.info("fixedDelayTask----- The first {} Individuals in {} Start toilet , Time consuming :{} second ", count + 1,
formatTime(),
timeConsuming);
Thread.sleep(timeConsuming * 1000L);
count++;
}
}
@Scheduled(cron = "0/5 * * * * ? ")
public void cronTask() throws InterruptedException {
if (count < 4) {
int timeConsuming = people[count];
log.info("cronTask----- The first {} Individuals in {} Start toilet , Time consuming :{} second ", count + 1,
formatTime(),
timeConsuming);
Thread.sleep(timeConsuming * 1000L);
count++;
}
}
@Scheduled(fixedRate = 5000)
public void fixedRateTask() throws InterruptedException {
if (count < 4) {
int timeConsuming = people[count];
log.info("fixedRateTask----- The first {} Individuals in {} Start toilet , Time consuming :{} second ", count + 1,
formatTime(),
timeConsuming);
Thread.sleep(timeConsuming * 1000L);
count++;
}
}
private String formatTime() {
return LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
}
}
The usage of these three modes is very simple , It is also used in a similar way . So what is the difference between them ? Now let's explain it through a five-star luxury public beta story .
The story background
There is a five-star luxury toilet somewhere , Everyone likes to go to the toilet here . Therefore, the supply of pits often exceeds the demand , Need to line up to go to the toilet . One day , There are four people queuing outside the toilet , The time everyone needs to go to the toilet is as follows :
- The first one 6 Second
- Second person 2 Second
- The third person 3 Second
- The fourth person 1 Second
Start to use the toilet from the first person .
fixedDelay Pattern
Log output :
fixedDelayTask----- The first 1 Individuals in 18:07:23 Start toilet , Time consuming :6 second
fixedDelayTask----- The first 2 Individuals in 18:07:34 Start toilet , Time consuming :2 second
fixedDelayTask----- The first 3 Individuals in 18:07:41 Start toilet , Time consuming :3 second
fixedDelayTask----- The first 4 Individuals in 18:07:49 Start toilet , Time consuming :1 second
@Scheduled(fixedDelay = 5000)
The toilet has a feature in this mode : Every time I use the toilet , Need to have 5 Seconds of toilet self-cleaning time , The toilet needs to be cleaned and disinfected , So as to ensure that it is still clean and hygienic when it is used next time . The implementation is shown in the figure :

- The first person is in 0 Start to go to the toilet in seconds ,6 Seconds later , Toilet needs 5 Second self-cleaning time ;
- The second person is in the 11 second (6+5) Start to use the toilet ,2 Seconds later , Toilet needs 5 Second self-cleaning time ;
- The third person is at 18 second (11+2+5) Start to use the toilet ,3 Seconds later , Toilet needs 5 Second self-cleaning time ;
- The fourth person is at 26 second (18+3+5) Start to use the toilet ,1 Seconds later …
Cron Pattern
Log output :
cronTask----- The first 1 Individuals in 18:09:15 Start toilet , Time consuming :6 second
cronTask----- The first 2 Individuals in 18:09:25 Start toilet , Time consuming :2 second
cronTask----- The first 3 Individuals in 18:09:30 Start toilet , Time consuming :3 second
cronTask----- The first 4 Individuals in 18:09:35 Start toilet , Time consuming :1 second
@Scheduled(cron = "0/5 * * * * ? ")
In this mode , The toilet only counts in seconds 5 Allow personnel to enter and use . Because through rigorous scientific analysis , It is found that the number of seconds is 5 The toilet experience is better when it is an integer multiple of , So only the current number of seconds is 5 Can only enter . also , Five star luxury public beta upgrade equipment , It can complete self-cleaning and disinfection at the moment of toileting , Therefore, there is no need for additional self-cleaning time , It also improves the utilization rate of toilets . The implementation is shown in the figure :

- The first person is 18:09:15 Start to use the toilet ,6 Seconds later (18:09:21) end , The next toilet auspicious time is 18:09:25;
- The second person is 18:09:25 Start to use the toilet ,2 Seconds later (18:09:27) end , The next toilet auspicious time is 18:09:30;
- The third person is 18:09:30 Start to use the toilet ,3 Seconds later (18:09:33) end , The next toilet auspicious time is 18:09:35;
- The fourth person is 18:09:35 Start to use the toilet ,1 Seconds later (18:09:36) end …
fixedRate Pattern
Log output :
fixedRateTask----- The first 1 Individuals in 18:10:18 Start toilet , Time consuming :6 second
fixedRateTask----- The first 2 Individuals in 18:10:24 Start toilet , Time consuming :2 second
fixedRateTask----- The first 3 Individuals in 18:10:28 Start toilet , Time consuming :3 second
fixedRateTask----- The first 4 Individuals in 18:10:33 Start toilet , Time consuming :1 second
@Scheduled(fixedRate = 5000)
After a long time of big data analysis , Come to a conclusion —— The best toilet time for people is 5 Second . So in this mode , Before people go to the toilet , The toilet will make a toilet plan in advance according to the number of people waiting , That is, each waiting person is assigned 5 Seconds toilet time . But there is a rule : When toileting ends in advance , Then the next person still needs to wait enough 5 Second ; And when the toilet worker times out , The person waiting for the toilet can go to the toilet immediately when the last person finishes . that :
The toilet plan is as follows :
- The first one : The first 0 Seconds into
- Second person : The first 5 Seconds into
- The third person : The first 10 Seconds into
- The fourth person : The first 15 Seconds into
According to the toilet time of everyone in the story background , The actual situation is shown in the figure :

- The first person is in 0 seconds , namely 18:10:18 Start to use the toilet ,6 Seconds later , Overtime , The second person goes to the toilet seamlessly ;
- The second person is in the 6 second (0+6) when , namely 18:10:24 Start toilet ,2 Seconds later , No timeout , The third person waits 2 second , Go to the toilet as scheduled ;
- The third person is at 10 second (6+2+2) when , namely 18:10:28 Start toilet ,3 Seconds later , No timeout , The fourth person waits 2 Second, go to the toilet as planned ;
- The fourth person is at 15 second (6+2+2+3+2) when , namely 18:10:33 Start toilet ,1 Seconds later …
Cron expression
Cron Pattern is the most powerful trigger strategy in timed tasks , Can cope with more situations . One Cron The expression has a total of 7 Elements , As shown in the following table :
| Time unit | If required | Value range | wildcard |
|---|---|---|---|
| second | yes | 0-59 The integer of | , - * / Four characters |
| branch | yes | 0-59 The integer of | , - * / Four characters |
| when | yes | 0-23 The integer of | , - * / Four characters |
| Japan | yes | 1-31 The integer of ( You need to consider the specific days of the month ) | ,- * ? / L W C Eight characters |
| month | yes | 1~12 Or JAN-DEC | , - * / Four characters |
| Zhou | yes | 1~7 Or SUN-SAT (1=SUN) | , - * ? / L C # Eight characters |
| year | no | 1970~2099 | , - * / Four characters |
More exclusive highlights are in my new book 《Spring Boot Interesting practical class 》 in .
边栏推荐
- How to verify accesstoken in oauth2 protocol
- HDU 4337 King Arthur&#39;s Knights 它输出一个哈密顿电路
- Uniapp adaptation problem
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
- Use of promise in ES6
- Oauth2协议中如何对accessToken进行校验
- Redis入門完整教程:問題定比特與優化
- oracle连接池长时间不使用连接失效问题
- 「小样本深度学习图像识别」最新2022综述
- 杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】
猜你喜欢

【基于 RT-Thread Studio的CPK-RA6M4 开发板环境搭建】

IDEA重启后无法创建Servlet文件的解决方案
![[cpk-ra6m4 development board environment construction based on RT thread studio]](/img/08/9a847c73d6da6fc74d84af56897752.png)
[cpk-ra6m4 development board environment construction based on RT thread studio]

【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程

Oauth2协议中如何对accessToken进行校验

The whole process of knowledge map construction

Household appliance industry under the "retail is king": what is the industry consensus?

The 8 element positioning methods of selenium that you have to know are simple and practical

Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform

Es6中Promise的使用
随机推荐
[secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
Redis入门完整教程:复制配置
mos管实现主副电源自动切换电路,并且“零”压降,静态电流20uA
掘金量化:通过history方法获取数据,和新浪财经,雪球同用等比复权因子。不同于同花顺
Qt蓝牙:QBluetoothDeviceInfo
ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法
制作(转换)ico图标
How to analyze fans' interests?
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
Utilisation de la promesse dans es6
uniapp的表单验证
Oauth2协议中如何对accessToken进行校验
2022 information security engineer examination outline
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
杰理之FM 模式单声道或立体声选择设置【篇】
INS/GPS组合导航类型简介
SQL Tuning Advisor一个错误ORA-00600: internal error code, arguments: [kesqsMakeBindValue:obj]
「小样本深度学习图像识别」最新2022综述
New benchmark! Intelligent social governance
Static proxy of proxy mode