Java中scheduleAtFixedRate的用法
作者:星辰_大海 时间:2022-10-10 11:07:37
scheduleAtFixedRate(task,time,period)
task-所要安排的任务 time-首次执行任务的时间 period-执行一次task的时间间隔,单位毫秒
作用:时间等于或超过time首次执行task,之后每隔period毫秒重复执行task
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
@Override
public void run() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
System.out.println("Current Time:"+format.format(calendar.getTime()));//获取当前系统时间
System.out.println("NO.1");
}
public static void main(String[] args) {
MyTimerTask task = new MyTimerTask();
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
System.out.println(format.format(calendar.getTime()));
calendar.add(Calendar.SECOND,3);//获取距离当前时间3秒后的时间
Timer timer = new Timer();
timer.scheduleAtFixedRate(task,calendar.getTime(),2000);
}
}
scheduleAtFixedRate(task, delay,period)
task-所要执行的任务 delay-执行任务的延迟时间,单位毫秒 period-执行一次task的时间间隔
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
@Override
public void run() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
System.out.println("Current Time:"+format.format(calendar.getTime()));//获取当前系统时间
System.out.println("NO.1");
}
public static void main(String[] args) {
MyTimerTask task = new MyTimerTask();
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
System.out.println(format.format(calendar.getTime()));
calendar.add(Calendar.SECOND,3);//获取距离当前时间3秒后的时间
Timer timer = new Timer();
//timer.scheduleAtFixedRate(task,calendar.getTime(),2000);
timer.scheduleAtFixedRate(task,1000,2000);
}
}
scheduleAtFixedRate和scheduleWithFixedDelay的区别
1)scheduleAtFixedRate:可以传入runnable,定制第一次的初始化执行时间,周期时间,单位时间-------创建并执行一个周期性任务,过了给定的初始延迟时间(1min执行一次or其他时间),会第一次被执行。执行过程中发生异常,任务停止。
2)scheduleWithFixedDelay:和上一种相类似-----创建并执行周期性任务,第一次执行及异常情况
A:1s中执行一个任务,每个任务执行时间500ms====两种无区别
B:执行任务时间过长,如间隔时间是1s,而执行时间3s====该情况下有区别,主要在周期时间的区别
scheduleWithFixedDelay该方法中一次任务执行时长超过周期时间,下一次任务会在该次任务执行结束时间基础上,计算执行延时(举例子:如间隔时间是1s,而执行时间3s,当前10:03时触发执行,理论而言,10:04时应当执行新任务,但通过该方法执行,执行结束时为10:06,在10:06的基础上延迟1s执行新任务)
scheduleAtFixedRate该方法(举例子:如间隔时间是1s,而执行时间3s,当前10:03时触发执行,10:06执行结束,10:04及10:05累积的任务会立刻执行,而非在10:06的基础上增加延时时间)
来源:https://blog.csdn.net/weixin_42103959/article/details/80433231
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android CoordinatorLayout高级用法之自定义Behavior
![](https://img.aspxhome.com/file/2023/3/91673_0s.gif)
使用java反射将结果集封装成为对象和对象集合操作
Java8新特性之精简的JRE详解_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/2/78302_0s.jpg)
C#实现文件压缩与解压的方法示例【ZIP格式】
详解Java对象结构与对象锁的升级
![](https://img.aspxhome.com/file/2023/8/64808_0s.png)
C++动态数组类的封装实例
Android实现基于滑动的SQLite数据分页加载技术(附demo源码下载)
Android通过原生APi获取所在位置的经纬度
一文带你熟练掌握Java中的日期时间相关类
![](https://img.aspxhome.com/file/2023/1/83031_0s.webp)
Android自定义View实现多边形统计图示例代码
![](https://img.aspxhome.com/file/2023/5/137335_0s.jpg)
JAVA遍历一个文件夹中的所有文件的小例子
Java 获取当前系统时间的三种方法
JavaSE实现猜拳游戏
Android自定义View实现水波纹引导动画
![](https://img.aspxhome.com/file/2023/5/129055_0s.gif)
maven多个仓库查询的优先级顺序案例讲解
SpringBoot 如何实时刷新静态文件
![](https://img.aspxhome.com/file/2023/5/58765_0s.png)
WPF ComboBox获取当前选择值的实例详解
C#中系统时间和UNIX时间戳互相转换
详解 Corba开发之Java实现Service与Client
![](https://img.aspxhome.com/file/2023/3/60183_0s.png)
Android应用中加入微信分享简单方法
![](https://img.aspxhome.com/file/2023/0/138560_0s.png)