Java定时任务详解

2.3、在延迟指定时间后以指定的间隔时间循环执行定时任务

public class TimerTest03 {

Timer timer;

public TimerTest03(){

timer = new Timer();

timer.schedule(new TimerTaskTest03(), 1000, 2000);

}

public static void main(String[] args) {

new TimerTest03();

}

}

public class TimerTaskTest03 extends TimerTask{

@Override

public void run() {

Date date = new Date(this.scheduledExecutionTime());

System.out.println(“本次执行该线程的时间为:” + date);

}

}

运行结果:

本次执行该线程的时间为:Tue Jun 10 21:19:47 CST 2014

本次执行该线程的时间为:Tue Jun 10 21:19:49 CST 2014

本次执行该线程的时间为:Tue Jun 10 21:19:51 CST 2014

本次执行该线程的时间为:Tue Jun 10 21:19:53 CST 2014

本次执行该线程的时间为:Tue Jun 10 21:19:55 CST 2014

本次执行该线程的时间为:Tue Jun 10 21:19:57 CST 2014

对于这个线程任务,如果我们不将该任务停止,他会一直运行下去。

对于上面三个实例,LZ只是简单的演示了一下,同时也没有讲解scheduleAtFixedRate方法的例子,其实该方法与schedule方法一样!

2.4、分析schedule和scheduleAtFixedRate

1、schedule(TimerTask task, Date time)、schedule(TimerTask task, long delay)

对于这两个方法而言,如果指定的计划执行时间scheduledExecutionTime<= systemCurrentTime,则task会被立即执行。scheduledExecutionTime不会因为某一个task的过度执行而改变。

2、schedule(TimerTask task, Date firstTime, long period)、schedule(TimerTask task, long delay, long period)

这两个方法与上面两个就有点儿不同的,前面提过Timer的计时器任务会因为前一个任务执行时间较长而延时。在这两个方法中,每一次执行的task的计划时间会随着前一个task的实际时间而发生改变,也就是scheduledExecutionTime(n+1)=realExecutionTime(n)+periodTime。也就是说如果第n个task由于某种情况导致这次的执行时间过程,最后导致systemCurrentTime>= scheduledExecutionTime(n+1),这是第n+1个task并不会因为到时了而执行,他会等待第n个task执行完之后再执行,那么这样势必会导致n+2个的执行实现scheduledExecutionTime放生改变即scheduledExecutionTime(n+2) = realExecutionTime(n+1)+periodTime。所以这两个方法更加注重保存间隔时间的稳定。

3、scheduleAtFixedRate(TimerTask task, Date firstTime, long period)、scheduleAtFixedRate(TimerTask task, long delay, long period)

在前面也提过scheduleAtFixedRate与schedule方法的侧重点不同,schedule方法侧重保存间隔时间的稳定,而scheduleAtFixedRate方法更加侧重于保持执行频率的稳定。为什么这么说,原因如下。在schedule方法中会因为前一个任务的延迟而导致其后面的定时任务延时,而scheduleAtFixedRate方法则不会,如果第n个task执行时间过长导致systemCurrentTime>= scheduledExecutionTime(n+1),则不会做任何等待他会立即执行第n+1个task,所以scheduleAtFixedRate方法执行时间的计算方法不同于schedule,而是scheduledExecutionTime(n)=firstExecuteTime +n*periodTime,该计算方法永远保持不变。所以scheduleAtFixedRate更加侧重于保持执行频率的稳定。

三、Timer的缺陷

3.1、Timer的缺陷

Timer计时器可以定时(指定时间执行任务)、延迟(延迟5秒执行任务)、周期性地执行任务(每隔个1秒执行任务),但是,Timer存在一些缺陷。首先Timer对调度的支持是基于绝对时间的,而不是相对时间,所以它对系统时间的改变非常敏感。其次Timer线程是不会捕获异常的,如果TimerTask抛出的了未检查异常则会导致Timer线程终止,同时Timer也不会重新恢复线程的执行,他会错误的认为整个Timer线程都会取消。同时,已经被安排单尚未执行的TimerTask也不会再执行了,新的任务也不能被调度。故如果TimerTask抛出未检查的异常,Timer将会产生无法预料的行为。

1、Timer管理时间延迟缺陷

前面Timer在执行定时任务时只会创建一个线程任务,如果存在多个线程,若其中某个线程因为某种原因而导致线程任务执行时间过长,超过了两个任务的间隔时间,会发生一些缺陷:

public class TimerTest04 {

private Timer timer;

public long start;

public TimerTest04(){

this.timer = new Timer();

start = System.currentTimeMillis();

}

public void timerOne(){

timer.schedule(new TimerTask() {

public void run() {

System.out.println(“timerOne invoked ,the time:” + (System.currentTimeMillis() - start));

try {

Thread.sleep(4000); //线程休眠3000

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}, 1000);

}

public void timerTwo(){

timer.schedule(new TimerTask() {

public void run() {

System.out.println(“timerOne invoked ,the time:” + (System.currentTimeMillis() - start));

}

}, 3000);

}

public static void main(String[] args) throws Exception {

TimerTest04 test = new TimerTest04();

test.timerOne();

test.timerTwo();

}

}

按照我们正常思路,timerTwo应该是在3s后执行,其结果应该是:

timerOne invoked ,the time:1001

timerOne invoked ,the time:3001

但是事与愿违,timerOne由于sleep(4000),休眠了4S,同时Timer内部是一个线程,导致timeOne所需的时间超过了间隔时间,结果:

timerOne invoked ,the time:1000

timerOne invoked ,the time:5000

2、Timer抛出异常缺陷

如果TimerTask抛出RuntimeException,Timer会终止所有任务的运行。如下:

public class TimerTest04 {

private Timer timer;

public TimerTest04(){

this.timer = new Timer();

}

public void timerOne(){

timer.schedule(new TimerTask() {

public void run() {

throw new RuntimeException();

}

}, 1000);

}

public void timerTwo(){

timer.schedule(new TimerTask() {

public void run() {

System.out.println(“我会不会执行呢??”);

}

}, 1000);

}

public static void main(String[] args) {

TimerTest04 test = new TimerTest04();

test.timerOne();

test.timerTwo();

}

}

运行结果:timerOne抛出异常,导致timerTwo任务终止。

Exception in thread “Timer-0” java.lang.RuntimeException

at com.chenssy.timer.TimerTest04$1.run(TimerTest04.java:25)

at java.util.TimerThread.mainLoop(Timer.java:555)

at java.util.TimerThread.run(Timer.java:505)

对于Timer的缺陷,我们可以考虑 ScheduledThreadPoolExecutor 来替代。Timer是基于绝对时间的,对系统时间比较敏感,而ScheduledThreadPoolExecutor 则是基于相对时间;Timer是内部是单一线程,而ScheduledThreadPoolExecutor内部是个线程池,所以可以支持多个任务并发执行。

3.2、用ScheduledExecutorService替代Timer

1、解决问题一:

public class ScheduledExecutorTest {

private ScheduledExecutorService scheduExec;

public long start;

ScheduledExecutorTest(){

this.scheduExec = Executors.newScheduledThreadPool(2);

this.start = System.currentTimeMillis();

}

public void timerOne(){

scheduExec.schedule(new Runnable() {

public void run() {

System.out.println(“timerOne,the time:” + (System.currentTimeMillis() - start));

try {

Thread.sleep(4000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

},1000,TimeUnit.MILLISECONDS);

}

public void timerTwo(){

scheduExec.schedule(new Runnable() {

public void run() {

System.out.println(“timerTwo,the time:” + (System.currentTimeMillis() - start));

}

},2000,TimeUnit.MILLISECONDS);

}

public static void main(String[] args) {

ScheduledExecutorTest test = new ScheduledExecutorTest();

test.timerOne();

test.timerTwo();

}

}

运行结果:

timerOne,the time:1003

timerTwo,the time:2005

2、解决问题二

public class ScheduledExecutorTest {

private ScheduledExecutorService scheduExec;

public long start;

ScheduledExecutorTest(){

this.scheduExec = Executors.newScheduledThreadPool(2);

this.start = System.currentTimeMillis();

}

public void timerOne(){

scheduExec.schedule(new Runnable() {

public void run() {

throw new RuntimeException();

}

},1000,TimeUnit.MILLISECONDS);

}

public void timerTwo(){

scheduExec.scheduleAtFixedRate(new Runnable() {

public void run() {

System.out.println(“timerTwo invoked …”);

}

},2000,500,TimeUnit.MILLISECONDS);

}

public static void main(String[] args) {

ScheduledExecutorTest test = new ScheduledExecutorTest();

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-MGZItLTg-1715732210587)]

[外链图片转存中…(img-VfxEuMQ7-1715732210587)]

[外链图片转存中…(img-QaRHS3Bm-1715732210587)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 13
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的定时任务可以使用Timer类来实现。Timer是Java自带的定时任务类,通过创建一个Timer对象,并使用schedule方法来指定任务的执行时间和执行频率。 示例代码如下: ```java TimerTask task = new TimerTask() { public void run() { System.out.println("当前时间: " + new Date() + "\n" + "线程名称: " + Thread.currentThread().getName()); } }; Timer timer = new Timer(); long delay = 1000L; timer.schedule(task, delay); ``` 上述代码将创建一个定时任务对象task,该任务将在1秒后开始执行,并且以指定的频率重复执行。任务的具体内容在run方法中定义,可以在其中编写需要执行的逻辑。 Timer内部使用一个叫做TaskQueue的类来存放定时任务。TaskQueue是一个基于最小堆实现的优先级队列,它会根据任务距离下一次执行时间的大小对任务进行排序,保证排在堆顶的任务最先执行。因此,在需要执行任务时,只需要从堆顶取出任务并执行即可。 需要注意的是,Timer类在多线程环境下存在一些问题,可能会导致任务执行的时间不准确。所以在Java中,更推荐使用java.util.concurrent包中的ScheduledExecutorService来实现定时任务。ScheduledExecutorService提供了更灵活和可靠的定时任务调度功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java 定时任务-最简单的3种实现方法](https://blog.csdn.net/qq_39731011/article/details/123332641)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Java 定时任务详解](https://blog.csdn.net/c630843901/article/details/128839445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值