当前位置:网站首页>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
剑指offer专项突击版第16天
干货!如何使用仪表构造SRv6-TE性能测试环境
认真对待每一个时刻
怀念故乡的月亮
律师解读 | 枪炮还是玫瑰?从大厂之争谈元宇宙互操作性
2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m
Introduction to the Elastic Stack
怀念故乡的面条
7月编程排行榜来啦!这次有何新变化?
随机推荐
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
Dart 命名参数语法
Pyspark机器学习:向量及其常用操作
干货!如何使用仪表构造SRv6-TE性能测试环境
Excel做题记录——整数规划优化模型
PMP 80个输入输出总结
MySQL3
PMP 项目沟通管理
6-23漏洞利用-postgresql代码执行利用
【愚公系列】2022年07月 Go教学课程 024-函数
【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
在互联网时代,有诸多「互联网+」模式的诞生
"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
Immutable
typescript27-枚举类型呢
请问shake数据库中为什么读取100个collection 后,直接就退出了,不继续读了呢?
智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
认真对待每一个时刻
What is a programming language
Introduction to Oracle