当前位置:网站首页>The difference between scheduleWithFixedDelay and scheduleAtFixedRate
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
2022-08-01 04:26:00 【无白发博深处】
scheduleWithFixedDelay使用
public class MainDemo {
public static int times = 0;
public static void main(String[] args) {
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
scheduledThreadPoolExecutor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
System.out.println( "开始执行时间" + System.currentTimeMillis());
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println( "执行结束时间" + System.currentTimeMillis());
}
},0,3, TimeUnit.SECONDS);
}
}
开始执行时间1658308204447
执行结束时间1658308208448
开始执行时间1658308211450
执行结束时间1658308215456
开始执行时间1658308218458
执行结束时间1658308222459
开始执行时间1658308225461
执行结束时间1658308229462
开始执行时间1658308232462
执行结束时间1658308236463
开始执行时间1658308239463
执行结束时间1658308243466
开始执行时间1658308246467
执行结束时间1658308250467
开始执行时间1658308253470
scheduleAtFixedRate的使用
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class MainDemo {
public static int times = 0;
public static void main(String[] args) {
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
scheduledThreadPoolExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
System.out.println( "开始执行时间" + System.currentTimeMillis());
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println( "执行结束时间" + System.currentTimeMillis());
}
},0,3, TimeUnit.SECONDS);
}
}
总结
scheduleAtFixedRateA function is after the program in the function body has finished executing,立即执行.
而scheduleWithFixedDelayAfter the program execution of the function body is completed,再delayExecute after set time.
边栏推荐
- PMP子过程定义总结
- Mysql基础篇(Mysql数据类型)
- Character encoding and floating point calculation precision loss problem
- Game Theory (Depu) and Sun Tzu's Art of War (42/100)
- lambda
- Basic Theoretical Knowledge of Software Testing - Use Cases
- 风险策略调优中重要的三步分析法
- 移动端页面秒开优化总结
- Dynamic Programming 01 Backpack
- "ArchSummit: The cry of the times, technical people can hear"
猜你喜欢
typescript27-枚举类型呢
MySQL3
Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
What is dynamic programming and what is the knapsack problem
MySQL3
Message queue design based on mysql
Elastic Stack的介绍
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
随机推荐
/etc/fstab
Message queue design based on mysql
How to promote new products online?
罗技鼠标体验记录
软件测试面试(三)
【无标题】
【愚公系列】2022年07月 Go教学课程 024-函数
Basic Theoretical Knowledge of Software Testing - Use Cases
A way to deal with infinite debugger
移动端页面秒开优化总结
typescript28-枚举类型的值以及数据枚举
请问shake数据库中为什么读取100个collection 后,直接就退出了,不继续读了呢?
Flutter “Hello world“ 程代码
RSA主要攻击方法
预言机简介
PMP子过程定义总结
剑指offer专项突击版第16天
typescript27-枚举类型呢
时时刻刻保持敬畏之心
出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法