Java实现Timer的定时调度函数schedule的四种用法
作者:星辰_大海 时间:2022-08-14 04:49:01
schedule的四种用法
schedule(task,time)
task-所安排的任务 time-执行任务的时间
作用:在时间等于或者超过time的时候执行且仅执行一次
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
private Integer cout = 0;
@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.schedule(task,calendar.getTime());
}
}
schedule(task,time,period)
task-所要安排执行的任务 time-首次执行任务的时间 period-执行一次task的时间间隔,单位毫秒
作用:时间等于或者超过time首次执行task,之后每隔period毫秒重复执行一次任务
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
private Integer cout = 0;
@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.schedule(task,calendar.getTime());
timer.schedule(task,calendar.getTime(),2000);
}
}
schedule(task,delay)
task-所要安排的任务 delay-执行任务前的延迟时间,单位毫秒
作用:等待delay毫秒后执行仅执行一次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.schedule(task,calendar.getTime());
//timer.schedule(task,calendar.getTime(),2000);
timer.schedule(task,2000);
}
}
schedule(task, delay,period)
作用:在等待delay毫秒后首次执行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.schedule(task,calendar.getTime());
//timer.schedule(task,calendar.getTime(),2000);
//timer.schedule(task,2000);
timer.schedule(task,2000,3000);
}
}
来源:https://blog.csdn.net/weixin_42103959/article/details/80432978
标签:Java,Timer,schedule
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java计算给定字符串中出现次数最多的字母和该字母出现次数的方法
2022-01-13 03:00:04
归并排序的原理及java代码实现
2021-11-18 13:51:10
![](https://img.aspxhome.com/file/2023/4/75484_0s.gif)
Android Build类的详解及简单实例
2023-07-02 21:12:45
SpringBoot构建RESTful API的实现示例
2022-04-13 14:45:08
搭建一个基础的Resty项目框架
2021-06-08 11:12:06
java 获取当前路径下的所有xml文档的方法
2021-08-08 13:54:29
Json操作库DynamicJson使用指南
2023-06-17 10:17:35
IDEA 2022 中的Lombok 使用基础教程
2023-04-09 21:57:09
![](https://img.aspxhome.com/file/2023/7/64547_0s.png)
java 数据结构并查集详解
2023-01-22 03:52:24
![](https://img.aspxhome.com/file/2023/5/71695_0s.png)
java读取XML文件的四种方法总结(必看篇)
2023-03-22 23:12:58
基于JDK动态代理原理解析
2022-07-24 19:43:33
Java8新特性之lambda(动力节点Java学院整理)
2022-01-16 21:35:54
Java 数据结构与算法系列精讲之哈希算法实现
2022-01-10 23:56:07
![](https://img.aspxhome.com/file/2023/3/71683_0s.gif)
Java基于Calendar类输出指定年份和月份的日历代码实例
2023-09-20 07:40:57
![](https://img.aspxhome.com/file/2023/4/67174_0s.png)
Java实现新建有返回值的线程的示例详解
2022-02-13 17:09:33
![](https://img.aspxhome.com/file/2023/0/61420_0s.jpg)
基于MapReduce实现决策树算法
2023-10-20 16:05:40
SpringBoot常用注解详细整理
2022-12-15 18:53:01
![](https://img.aspxhome.com/file/2023/3/75763_0s.jpg)
使用@Order控制配置类/AOP/方法/字段的加载顺序详解
2023-09-05 17:34:35
![](https://img.aspxhome.com/file/2023/1/58421_0s.png)
自己动手写的mybatis分页插件(极其简单好用)
2023-11-01 18:12:09
c# AcceptEx与完成端口(IOCP)结合的示例
2023-07-29 01:46:24