Timer的缺陷(四)

先定义一个TimerTask类

package lxd.timer.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimerTask;

public class MyTimerTask2 extends TimerTask {
    private String name;

    private long costTime;

    public MyTimerTask2(String name, long costTime) {
        this.name = name;
        this.costTime = costTime;
    }


    @Override
    public void run() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(name + "'s current exec time is: " 
                + sf.format(calendar.getTime()));
        try {
            Thread.sleep(costTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        calendar = Calendar.getInstance();
        System.out.println(name + "'s finish time is: " 
                + sf.format(calendar.getTime()));
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getCostTime() {
        return costTime;
    }

    public void setCostTime(long costTime) {
        this.costTime = costTime;
    }

}

管理并发任务的缺陷

  • Timer有且仅有一个线程去执行定时任务,如果存在多个任务,且任务执行时间过长,会导致执行效果与预期不符。

测试demo:

package lxd.timer.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;

/**
 * 
 * Timer的缺陷demo
 * 
 * <pre>
 * Timer的缺陷demo,不支持并发
 * </pre>
 * 
 * @author 李晓东
 * 
 * 2017.05.28
 * 
 * @since 1.0
 *
 */
public class MyTimer2 {

    public static void main(String[] args) {
        Timer timer = new Timer();
        MyTimerTask2 task1 = new MyTimerTask2("No.1", 2000);
        MyTimerTask2 task2 = new MyTimerTask2("No.2", 2000);

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("current time is: " + sf.format(calendar.getTime()));
//      timer.schedule(task1, calendar.getTime());
//      timer.schedule(task2, calendar.getTime());
        timer.scheduleAtFixedRate(task1, calendar.getTime(), 2000);
        timer.scheduleAtFixedRate(task2, calendar.getTime(), 2000);
    }

}

测试结果:

current time is: 2017-05-28 15:58:07
No.1's current exec time is: 2017-05-28 15:58:07
No.1's finish time is: 2017-05-28 15:58:09
No.2's current exec time is: 2017-05-28 15:58:09
No.2's finish time is: 2017-05-28 15:58:11
No.2's current exec time is: 2017-05-28 15:58:11
No.2's finish time is: 2017-05-28 15:58:13
No.1's current exec time is: 2017-05-28 15:58:13
No.1's finish time is: 2017-05-28 15:58:15
No.1's current exec time is: 2017-05-28 15:58:15

以上schedule和scheduleAtFixedRate的效果是一样的,Timer不支持并发。

当任务抛出异常时的缺陷

  • 如果TimerTask抛出RuntimeException,Timer会停止所有任务的运行。

测试demo,在MyTimerTask2的run方法中加上一句话:

@Override
public void run() {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(name + "'s current exec time is: " 
            + sf.format(calendar.getTime()));
    try {
        Thread.sleep(costTime);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    calendar = Calendar.getInstance();
    System.out.println(name + "'s finish time is: " 
            + sf.format(calendar.getTime()));
    //加上测试运行时异常
    throw new RuntimeException();
}

MyTimer2不变,测试结果:

current time is: 2017-05-28 16:05:35
No.1's current exec time is: 2017-05-28 16:05:35
No.1's finish time is: 2017-05-28 16:05:37
Exception in thread "Timer-0" java.lang.RuntimeException
    at lxd.timer.demo.MyTimerTask2.run(MyTimerTask2.java:32)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

Timer的使用禁区

  • 对时效性要求较高的多任务并发作业

- 对复杂任务的调度

这个时候需要使用Quartz了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值