定时任务详解

1、定时任务

在公司做项目时,经常遇到要用到定时任务的事情,但是对定时任务不熟练的时候会出现重复任务的情况,不深入,这次将定时任务好好学习分析一下

定时任务的原理

假如我将一个现场通过不断 轮询的方式去判断,就能实现定时任务功能

public class Task {
   

    public static void main(String[] args) {
   
        // run in a second
        final long timeInterval = 1000;
        Runnable runnable = new Runnable() {
   
            @Override
            public void run() {
   
                while (true) {
   
                    System.out.println("Hello !!");
                    try {
   
                        Thread.sleep(timeInterval);
                    } catch (InterruptedException e) {
   
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
}

这里解释下InterruptedException异常

 
Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted,
 either before or during the activity. Occasionally a method may wish to test whether the current
  thread has been interrupted, and if so, to immediately throw this exception.
   The following code can be used to achieve this effect:

简单来说就是当阻塞方法收到中断请求的时候就会抛出InterruptedException异常,当一个方法后面声明可能会抛出InterruptedException 异常时,说明该方法是可能会花一点时间,但是可以取消的方法。

抛InterruptedException的代表方法有:sleep(),wait(),join()
执行wait方法的线程,会进入等待区等待被notify/notify All。在等待期间,线程不会活动。

执行sleep方法的线程,会暂停执行参数内所设置的时间。

执行join方法的线程,会等待到指定的线程结束为止。

因此,上面的方法都是需要花点时间的方法。这三个方法在执行过程中会不断轮询中断状态(interrupted方法),从而自己抛出InterruptedException。

interrupt方法其实只是改变了中断状态而已。

所以,如果在线程进行其他处理时,调用了它的interrupt方法,线程也不会抛出InterruptedException的,只有当线程走到了sleep, wait, join这些方法的时候,才会抛出InterruptedException。若是没有调用sleep, wait, join这些方法,或者没有在线程里自己检查中断状态,自己抛出InterruptedException,那InterruptedException是不会抛出来的。

更多的知识点可以参考这篇文章InterruptedException需要注意的问题

Timer实现

Java在1.3版本引入了Timer工具类,它是一个古老的定时器,搭配TimerTask和TaskQueue一起使用,示例

public class TimeTaskTest {
   
    public static void main(String[] args) {
   
        TimerTask timerTask = new TimerTask() {
   
            @Override
            public void run() {
   
                System.out.println("hell world");
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask, 10, 3000);
    }
}
//Timer类
public void schedule(TimerTask task, long delay, long period) {
   
        if (delay < 0)
            throw new IllegalArgumentException("Negative delay.");
        if (period <= 0)
            throw new IllegalArgumentException("Non-positive period.");
        sched(task, System.currentTimeMillis()+delay, -period);
    }
private void sched(TimerTask task, long time, long period) 
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值