java当中的定时器的4种使用方式
作者:shangke 时间:2022-08-07 14:31:39
对于开发游戏项目的同胞来说,Timer 这个东西肯定不会陌生,今天对以前自己经常使用的定时进行了一番小小的总结!没有写具体实现的原理,只是列举出了其中的四种比较常见的使用方法,相对而言,所以只要按照其所列举的例子仿照即可!
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimeTest {
public static void main(String[] args) {
timer1();
//timer2();
//timer3();
//timer4();
}
// 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)
public static void timer1() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 2000);// 设定指定的时间time,此处为2000毫秒
}
// 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
// schedule(TimerTask task, long delay, long period)
public static void timer2() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 1000, 5000);
}
// 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
// scheduleAtFixedRate(TimerTask task, long delay, long period)
public static void timer3() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, 1000, 2000);
}
// 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
// Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
public static void timer4() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时
calendar.set(Calendar.MINUTE, 0); // 控制分
calendar.set(Calendar.SECOND, 0); // 控制秒
Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的12:00:00
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
}
}
标签:java,定时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringSecurity+Redis认证过程小结
2021-07-21 11:38:11
![](https://img.aspxhome.com/file/2023/3/71653_0s.png)
Java SoftReference类案例详解
2023-04-07 06:52:29
浅析SpringBoot2.4 静态资源加载问题
2023-01-29 11:04:33
![](https://img.aspxhome.com/file/2023/8/64158_0s.png)
新手小白学JAVA IDEA下载使用手册全集
2023-11-23 14:33:23
![](https://img.aspxhome.com/file/2023/3/59773_0s.jpg)
Java ThreadPoolExecutor的参数深入理解
2022-08-29 10:08:39
gson对象序列化的示例
2023-11-25 08:54:28
![](https://img.aspxhome.com/file/2023/6/59956_0s.png)
Flutter中嵌入Android 原生TextView实例教程
2023-07-05 02:02:00
![](https://img.aspxhome.com/file/2023/5/83515_0s.png)
java实现订餐系统
2023-08-12 04:39:06
Java拷贝数组方法Arrays.copyOf()是地址传递的证明实例
2023-11-08 11:51:29
![](https://img.aspxhome.com/file/2023/5/58215_0s.png)
java读取文件字符集示例方法
2023-11-09 12:35:39
详解spring boot引入外部jar包的坑
2021-07-29 10:49:49
![](https://img.aspxhome.com/file/2023/4/61314_0s.jpg)
java 二分法算法的实例
2023-04-25 05:04:05
Jersey Restful接口如何获取参数的问题
2023-10-29 14:44:16
详解Spring依赖注入的三种方式使用及优缺点
2023-06-09 18:29:00
![](https://img.aspxhome.com/file/2023/6/61046_0s.png)
Java面向对象程序设计:类的定义,静态变量,成员变量,构造函数,封装与私有,this概念与用法详解
2022-07-12 03:35:14
![](https://img.aspxhome.com/file/2023/5/61885_0s.png)
一文详解Java中Stream流的使用
2021-08-23 11:54:51
![](https://img.aspxhome.com/file/2023/0/64800_0s.png)
java用户名密码验证示例代码分享
2021-10-14 08:16:14
springboot实现异步任务
2023-04-23 01:25:21
![](https://img.aspxhome.com/file/2023/0/64210_0s.jpg)
深入C#中使用SqlDbType.Xml类型参数的使用详解
2022-07-24 13:56:18
Java使用Redisson分布式锁实现原理
2023-12-10 11:07:30
![](https://img.aspxhome.com/file/2023/1/68141_0s.png)