当前位置:网站首页>scheduleWithFixedDelay和scheduleAtFixedRate的区别
scheduleWithFixedDelay和scheduleAtFixedRate的区别
2022-08-01 04:14: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);
}
}

总结
scheduleAtFixedRate函数是在函数体里的程序执行完毕之后,立即执行.
而scheduleWithFixedDelay是在函数体能的程序执行完成之后,再delay设置的时间后执行。
边栏推荐
- 出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法
- Advice given by experts with four years of development experience in Flutter tutorial
- The Flow Of Percona Toolkit pt-table-checksum
- Character encoding and floating point calculation precision loss problem
- 阿叶的目标
- Introduction to the Elastic Stack
- 数据比对功能调研总结
- 云服务器下载安装mongo数据库并远程连接详细图文版本(全)
- Open source project site must-have & communication area function
- leetcode6132. Make all elements in an array equal to zero (simple, weekly)
猜你喜欢

Message queue design based on mysql

软件测试周刊(第82期):其实所有纠结做选择的人心里早就有了答案,咨询只是想得到内心所倾向的选择。

Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)

Make your Lottie support word wrapping in text fields

Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink

button去除黑框

怀念故乡的月亮

Interview Blitz 69: Is TCP Reliable?Why?

Visual Studio提供的 Command Prompt 到底有啥用

【消息通知】用公众号模板消息怎么样?
随机推荐
软件测试基础理论知识—用例篇
数组问题之《两数之和》以及《三数之和 》
Mysql基础篇(约束)
这个地图绘制工具太赞了,推荐~~
简单易用的任务队列-beanstalkd
Flink 1.13 (8) CDC
出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法
【堆】小红的数组
How to promote new products online?
开源许可证 GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
Unknown Bounded Array
PMP 项目沟通管理
剑指offer专项突击版第16天
win10 固定本机IP
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
【消息通知】用公众号模板消息怎么样?
【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)
What is dynamic programming and what is the knapsack problem
Error using ts-node
How to download the Keil package