当前位置:网站首页>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.
边栏推荐
- TypeScript simplifies running ts-node
- EntityFramework saves to SQLServer decimal precision is lost
- 【无标题】
- 在互联网时代,有诸多「互联网+」模式的诞生
- PMP工具与技术总结
- How to promote new products online?
- typescript19-对象可选参数
- Dart named parameter syntax
- 软件测试面试(三)
- "Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
猜你喜欢
随机推荐
[uniCloud] Application and Improvement of Cloud Objects
Introduction to the Elastic Stack
高数 | 【重积分】线面积分880例题
出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法
这里有110+公开的专业数据集
typescript19-对象可选参数
typescript21-接口和类型别名的对比
Interview Blitz 69: Is TCP Reliable?Why?
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
风险策略调优中重要的三步分析法
Input input box cursor automatically jumps to the last bug after the previous input
The 16th day of the special assault version of the sword offer
使用ts-node报错
Error using ts-node
Visual Studio提供的 Command Prompt 到底有啥用
IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints
PMP工具与技术总结
Flink 1.13 (8) CDC
【愚公系列】2022年07月 Go教学课程 024-函数
How to promote new products online?









