当前位置:网站首页>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.
边栏推荐
- The kernel's handling of the device tree
- Difference Between Compiled and Interpreted Languages
- leetcode6133. 分组的最大数量(中等)
- 解决ffmpeg使用screen-capture-recorder录屏,有屏幕缩放的情况下录不全的问题
- 2. # code comments
- [FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
- 数组问题之《下一个排列》、《旋转图像》以及二分查找之《搜索二维矩阵》
- Simple and easy to use task queue - beanstalkd
- Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
- Dart 命名参数语法
猜你喜欢

Software Testing Interview (3)

Input input box cursor automatically jumps to the last bug after the previous input

leetcode6133. 分组的最大数量(中等)

博客系统(完整版)

怀念故乡的月亮

对无限debugger的一种处理方式

The 16th day of the special assault version of the sword offer

safari浏览器怎么导入书签

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

JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
随机推荐
Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
博客系统(完整版)
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
leetcode:126. Word Solitaire II
Valentine's Day Romantic 3D Photo Wall [with source code]
风险策略调优中重要的三步分析法
[Getting Started Tutorial] Rollup Module Packager Integration
PMP工具与技术总结
【无标题】
FFmpeg 搭建本地屏幕录制环境
Make your Lottie support word wrapping in text fields
lambda
Flutter Tutorial 02 Introduction to Flutter Desktop Program Development Tutorial Run hello world (tutorial includes source code)
预言机简介
[uniCloud] Application and Improvement of Cloud Objects
Interview Blitz 69: Is TCP Reliable?Why?
The 16th day of the special assault version of the sword offer
Mysql中的数据类型和运算符
Dynamic Programming 01 Backpack