当前位置:网站首页>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 .
边栏推荐
- Appx代码签名指南
- Software testing -- common assertions of JMeter interface testing
- MySQL is an optimization artifact to improve the efficiency of massive data query
- OC, OD, push-pull explanation of hardware
- 又一百万量子比特!以色列光量子初创公司完成1500万美元融资
- 杰理之FM 模式单声道或立体声选择设置【篇】
- How to verify accesstoken in oauth2 protocol
- 应用程序启动速度的优化
- Uniapp adaptation problem
- Redis入门完整教程:复制原理
猜你喜欢
[cpk-ra6m4 development board environment construction based on RT thread studio]
centerX: 用中国特色社会主义的方式打开centernet
Es6中Promise的使用
[socket] ① overview of socket technology
Redis getting started complete tutorial: replication topology
mos管實現主副電源自動切換電路,並且“零”壓降,靜態電流20uA
uniapp适配问题
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
Redis入门完整教程:RDB持久化
Redis入门完整教程:复制原理
随机推荐
【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
leetcode
SQL中删除数据
Redis introduction complete tutorial: client case analysis
netperf 而网络性能测量
A complete tutorial for getting started with redis: RDB persistence
Redis getting started complete tutorial: client management
杰理之发射端在接收端关机之后假死机【篇】
uniapp的表单验证
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
制作(转换)ico图标
Oracle connection pool is not used for a long time, and the connection fails
Hazel engine learning (V)
Use of tensorboard
【2022国赛模拟】多边形——计算几何、二分答案、倍增
杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
Appx代码签名指南
Use of promise in ES6
Redis introduction complete tutorial: replication principle
Redis入门完整教程:问题定位与优化