当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
【无标题】
请问表格储存中用sql只能查询到主键列,ots sql非主键不支持吗?
深圳某游戏研发公司给每个工位都装监控,网友:堪比坐牢!
How to promote new products online?
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
Immutable
scheduleWithFixedDelay和scheduleAtFixedRate的区别
智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
[Search topic] After reading the inevitable BFS solution to the shortest path problem
Flutter “Hello world“ 程代码
PMP 80个输入输出总结
PMP工具与技术总结
基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
最新 955 不加班的公司名单
【愚公系列】2022年07月 Go教学课程 025-递归函数
The maximum quantity leetcode6133. Grouping (medium)
【堆】小红的数组
Dart 命名参数语法
Message queue design based on mysql








