当前位置:网站首页>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设置的时间后执行。
边栏推荐
- MySQL4
- "ArchSummit: The cry of the times, technical people can hear"
- Flutter Tutorial 02 Introduction to Flutter Desktop Program Development Tutorial Run hello world (tutorial includes source code)
- New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment
- 6-23漏洞利用-postgresql代码执行利用
- A way to deal with infinite debugger
- Unknown Bounded Array
- 基于Arduino制作非接触式测温仪
- Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
- PMP 项目沟通管理
猜你喜欢
6-23漏洞利用-postgresql代码执行利用
MySQL3
leetcode:126. Word Solitaire II
Step by step hand tearing carousel Figure 3 (nanny level tutorial)
使用ts-node报错
MySQL3
【入门教程】Rollup模块打包器整合
微软 Win10 照片磁贴后的又一“刺客”,谷歌 Chrome 浏览器将在新标签页展示用户照片
【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)
How to promote new products online?
随机推荐
项目越写越大,我是这样做拆分的
Simple and easy to use task queue - beanstalkd
【愚公系列】2022年07月 Go教学课程 025-递归函数
MySQL modifies SQL statements to optimize performance
Input输入框光标在前输入后自动跳到最后面的bug
One service layer needs to call the other two service layers to obtain data and assemble it into the final data. The data is all lists. How to design the cache?
2. # code comments
简单易用的任务队列-beanstalkd
出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法
对无限debugger的一种处理方式
动态规划 01背包
Mysql基础篇(Mysql数据类型)
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
Introduction to the Elastic Stack
August 22 Promotion Ambassador Extra Reward Rules
What is a programming language
Flutter “Hello world“ 程代码
Introduction to Oracle
win10 固定本机IP
Flutter "Hello world" program code