• Timer的异常


      定时任务用Timer实现有可能出现异常,因为它是基于绝对时间而不是相对时间进行调度的。当环境的系统时间被修改后,原来的定时任务可能就不跑了。另外需要注意一点,捕获并处理定时任务的异常。如果在TimerTask里抛出了异常,那么Timer认为定时任务被取消并终止执行线程。举例:

    package com.wulinfeng.concurrent;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class OutOfTime {
    
        public static void main(String[] args) throws InterruptedException {
            Timer timer = new Timer();
    
            // 设置一个一次性的定时任务,1秒后执行
            timer.schedule(new PrintTask(), 1000);
    
            // 休眠一秒
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            // 设置一个一次性任务,间隔1秒执行
            timer.schedule(new ThrowTask(), 1000);
    
            // 休眠一秒
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            // 再设置一个周期性任务,可是不会再来了
            timer.schedule(new PrintTask(), 1000, 1000);
        }
    
        static class PrintTask extends TimerTask {
    
            @Override
            public void run() {
                System.out.println("I'm coming...");
            }
    
        }
    
        static class ThrowTask extends TimerTask {
    
            @Override
            public void run() {
                throw new RuntimeException();
            }
    
        }
    }

      运行结果:

    I'm coming...
    Exception in thread "Timer-0" java.lang.RuntimeException
        at com.wulinfeng.concurrent.OutOfTime$ThrowTask.run(OutOfTime.java:50)
        at java.util.TimerThread.mainLoop(Timer.java:555)
        at java.util.TimerThread.run(Timer.java:505)
    Exception in thread "main" java.lang.IllegalStateException: Timer already cancelled.
        at java.util.Timer.sched(Timer.java:397)
        at java.util.Timer.schedule(Timer.java:248)
        at com.wulinfeng.concurrent.OutOfTime.main(OutOfTime.java:34)

      如果注掉这一行,那么定时任务还是会一直跑下去的

    timer.schedule(new ThrowTask(), 1000);
  • 相关阅读:
    SpringMVC
    MyBatis 与 Spring 的完美整合方法
    持久层之 MyBatis: 第三篇 :缓存 And 高级查询
    持久层之 MyBatis: 第二篇 :动态SQL And多表查询
    C语言实现简单epoll服务器(二)
    C语言简单实现epoll服务器(一)
    shell编程题(十九)
    shell编程题(十七)
    shell编程题(十六)
    shell编程题(十三)
  • 原文地址:https://www.cnblogs.com/wuxun1997/p/6822904.html
Copyright © 2020-2023  润新知