java实现定时器的四种方式
1. 使用thread.sleep()方法
thread.sleep()方法可以让当前线程暂停执行一段时间,我们可以利用它来实现简单的定时器。
示例代码:
```java
public class timerdemo1 {
public static void main(string[] args) throws interruptedexception {
int count = 0;
while (true) {
system.out.println(定时器已经运行了 + (++count) + 秒);
thread.sleep(1000); // 暂停1000毫秒,即1秒
}
}
}
```
该示例中我们使用了一个无限循环,每次循环输出当前运行的时间,并暂停1秒钟后再继续。
优点:实现简单,易于理解。
缺点:无法在定时器运行过程中修改定时器的时间或停止定时器,不适合实现复杂的定时器逻辑。
2. 使用timer类
timer类是java提供的一个简单的定时器工具,可以轻松实现定时器的开启、停止、暂停等操作。
示例代码:
```java
import java.util.timer;
import java.util.timertask;
public class timerdemo2 {
public static void main(string[] args) throws interruptedexception {
timer timer = new timer();
int count = 0;
timer.schedule(new timertask() {
public void run() {
system.out.println(定时器已经运行了 + (++count) + 秒);
}
}, 1000, 1000);
thread.sleep(5000);
timer.cancel();
}
}
```
该示例中我们使用了timer类实现定时器的功能,使用timer.schedule()方法开启定时器,可以设定初始延迟时间、定时器间隔时间,并通过timer.cancel()方法停止定时器。
优点:使用方便,适合实现一些简单的定时任务。
缺点:无法在定时器运行过程中修改定时器的时间或停止定时器,不支持并发操作。
3. 使用scheduledexecutorservice类
scheduledexecutorservice类是java提供的用于定时执行任务的工具类,可以设定定时器的初始延迟时间、间隔时间、并发量等参数,支持更加灵活的定时器条件设定和停止。
示例代码:
```java
import java.util.concurrent.executors;
import java.util.concurrent.scheduledexecutorservice;
import java.util.concurrent.timeunit;
public class timerdemo3 {
public static void main(string[] args) throws interruptedexception {
scheduledexecutorservice executor = executors.newscheduledthreadpool(1); // 开启一个线程池
int count = 0;
executor.scheduleatfixedrate(() -> system.out.println(定时器已经运行了 + (++count) + 秒), 1, 1, timeunit.seconds);
thread.sleep(5000);
executor.shutdownnow();
}
}
```
该示例中我们使用了scheduledexecutorservice类实现定时器的功能,在executor.scheduleatfixedrate()方法中设定了初始延迟时间、定时器间隔时间,并通过executor.shutdownnow()方法停止定时器。
优点:灵活且方便,支持并发操作,适合实现一些复杂的定时任务。
缺点:算法略有复杂,需要一定的背景知识。
4. 使用quartz类
quartz是java中一个强大的定时器框架,支持各种复杂的计划和时间设定,适用于大型项目中的复杂定时任务。
示例代码:
```java
import org.quartz.job;
import org.quartz.jobexecutioncontext;
import org.quartz.jobexecutionexception;
import org.quartz.scheduler;
import org.quartz.schedulerexception;
import org.quartz.impl.stdschedulerfactory;
import static org.quartz.jobbuilder.newjob;
import static org.quartz.triggerbuilder.newtrigger;
import static org.quartz.simpleschedulebuilder.simpleschedule;
import static org.quartz.cronschedulebuilder.cronschedule;
public class timerdemo4 {
public static void main(string[] args) throws schedulerexception, interruptedexception {
scheduler scheduler = stdschedulerfactory.getdefaultscheduler(); // 获取调度器
scheduler.start();
job myjob = new job() { // 自定义任务
public void execute(jobexecutioncontext arg0) throws jobexecutionexception {
system.out.println(调度器运行中...);
}
};
scheduler.schedulejob(
newjob(myjob.getclass()) // 设置任务
.withidentity(myjob, group1)
.build(),
newtrigger()
.withidentity(myjobtrigger, group1)
.withschedule(
simpleschedule() // 不重复执行
.withintervalinseconds(1)
.withrepeatcount(0)
)
.build()
);
thread.sleep(5000);
scheduler.shutdown();
}
}
```
该示例中我们使用了quartz框架实现定时器,创建一个自定义的任务,并调度它的执行。我们可以通过修改trigger实现更加复杂的时间调度设定。
优点:可以实现各种复杂的计划和时间设定,适用于大型项目中的复杂定时任务。
缺点:相对于其他方案来说,quartz的学习和上手成本比较高。
小米重拳出击全新发布多款新品
选购串口服务器的3个标准
基于Proteus软件和AT80C51单片机实现流水灯的仿真设计
超声波模块HC-SR04电路测量原理讲解
这是你所不知道的:vivo Xplay6还是一台夜拍神器
java实现定时器的四种方式
小米MIUI9最新消息:小米新机明日发布,小米MIUI9锁屏界面却提前曝光,MIUI9明日会有吗?
疫情对全球半导体市场有多大扰动?
上海鸣志电器关于收购Technosoft Motion AG公司100%股权的公告
电路中什么叫噪声电压?什么叫干扰电压?
AR手游《哈利波特:巫师联盟》已于今日上线,支持iOS及安卓设备
Vishay将PHP系列精密高功率薄膜片式电阻的外形尺寸扩展至0603到0805
ARM CSS N2处理器解析
鸿海集团的量子计算机项目开始浮出水面
Futurus展示全新MR全景显示技术,适配于各类汽车的挡风玻璃
数显拉力试验机官网的发展趋势和重要性,试验机老二给你说说!
无线充电技术会对人们的身体造成伤害吗
压力传感器发展过程的下四个阶段解析
小米6的2个升级功能已公布,还有2个即将揭晓
入侵探测装置的软硬件设计