当前位置:网站首页>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.
边栏推荐
猜你喜欢
黑客到底可以厉害到什么程度?
Message queue MySQL table for storing message data
对无限debugger的一种处理方式
[Search topic] After reading the inevitable BFS solution to the shortest path problem
Elastic Stack的介绍
Visual Studio提供的 Command Prompt 到底有啥用
typescript26-字面量类型
ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks
最新 955 不加班的公司名单
微软 Win10 照片磁贴后的又一“刺客”,谷歌 Chrome 浏览器将在新标签页展示用户照片
随机推荐
请问表格储存中用sql只能查询到主键列,ots sql非主键不支持吗?
Valentine's Day Romantic 3D Photo Wall [with source code]
API设计笔记:pimpl技巧
微软 Win10 照片磁贴后的又一“刺客”,谷歌 Chrome 浏览器将在新标签页展示用户照片
typescript26-字面量类型
typescript21-接口和类型别名的对比
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
Excel做题记录——整数规划优化模型
PMP 项目质量管理
Take you to experience a type programming practice
博客系统(完整版)
leetcode6133. 分组的最大数量(中等)
Dart named parameter syntax
Basic Theoretical Knowledge of Software Testing - Use Cases
What is dynamic programming and what is the knapsack problem
让你的 Lottie 支持文字区域内自动换行
lambda
一个往年的朋友
软件测试基础理论知识—用例篇
MySQL3