1.0-Timer(小弟)

2017-12-09  本文已影响0人  王子也寂寞

一.定义

二.简单使用

首先创建TimerTask类,实现业务逻辑的run方法.

private String name;
    public MyTimerTask(String inputname) {
        this.name = inputname;
    }

    public String getName() {
        return name;
    }

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

    public void run() {
        System.out.println("Current name is:" + name);
    }

通过timer调用task来定时定频率的执行

//1.创建一个Timer实例
        Timer timer = new Timer();
        //2.创建一个MyTimerTask实例
        MyTimerTask myTimerTask = new MyTimerTask("No.1");
        //3.通过timer定时定频率调用myTimerTask的业务逻辑
        //第一次执行实在当前时间的两秒后,每隔1秒执行一次.
        timer.schedule(myTimerTask, 2000L, 1000L);

三.schedule的四种用法

1.schedule的前两种用法

a.规定时间后执行一次

//1.创建一个Timer实例
        Timer timer = new Timer();
        //2.创建一个MyTimerTask实例
        MyTimerTask myTimerTask = new MyTimerTask("No.1");
        //3.通过timer定时定频率调用myTimerTask的业务逻辑
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sf.format(calendar.getTime()));
        calendar.add(Calendar.SECOND, 3);
        myTimerTask.setName("schedule1");
        //三秒后执行一次
        timer.schedule(myTimerTask, calendar.getTime());

b.时间等于或超过time首次执行task,之后每隔period毫秒重复执行一次task

//三秒后执行一次,之后每隔2秒重复执行一次task
        timer.schedule(myTimerTask, calendar.getTime(), 2000L);

2.schedule的后两种方法

a.参数不是time而是delay

//三秒后执行一次
        timer.schedule(myTimerTask, 3000L);

b.参数不是time而是delay

//三秒后执行一次,之后每隔2秒重复执行一次task
        timer.schedule(myTimerTask, 3000L,2000L);

四.scheduleAtFixedRate的两种用法

1.time参数

三个参数

2.delay参数

五.其他函数

1.TimerTask的cancel(),scheduledExecutionTime()

思路是在TimerTask列中,创建一个属性,记录重复次数,然后在run()方法中判断这个次数,当想让停止的时候调用cancel()方法.就会取消定时任务.

2.Timer的cancel(),purge()

六.schedule和scheduleAtFixedRate的区别

两种情况看区别

七.综合应用

跳舞机器人和灌水机器人的例子

public class DancingRobot extends TimerTask {

    public void run() {
        //获取最近的一次任务执行的时间并将其格式化
        SimpleDateFormat sf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        System.out.println("Scheduled exec time is:" + sf.format(scheduledExecutionTime()));
        System.out.println("Dancing happily!");
    }
}
public class WaterRobot extends TimerTask {

    //最大容量为5
    private Integer bucketCapacity = 0;

    private Timer timer;
    public WaterRobot(Timer inputTimer) {
        this.timer = inputTimer;
    }

    public void run() {
        //灌水,直到桶满为止
        if (bucketCapacity < 5) {
            System.out.println("Add 1 water into the bucket!");
            bucketCapacity ++;
        } else {
            //水满就停止执行.
            System.out.println("The number of canceled task in timer is:" + timer.purge());
            cancel();
            System.out.println("The waterRobot has been aborted");
            System.out.println("The number of canceled task in timer is:" + timer.purge());
            System.out.println("Current water is:" + bucketCapacity);
            //等待两秒钟,终止timer里面的所有内容
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            timer.cancel();
            System.out.println("The number of canceled task in timer is:" + timer.purge());
        }

    }
}
public class Executor {
    public static void main(String[] args) {
        Timer timer = new Timer();
        //获取当前的时间
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        System.out.println("Current time is:" + sf.format(calendar.getTime()));
        DancingRobot dr = new DancingRobot();
        WaterRobot wr = new WaterRobot(timer);

        timer.schedule(dr, calendar.getTime(), 2000);
        timer.scheduleAtFixedRate(wr, calendar.getTime(), 1000);
    }
}

八.Timer的缺陷

1. 管理并发任务的缺陷

2. 任务抛出异常时的缺陷

九.Timer的使用禁区

上一篇 下一篇

猜你喜欢

热点阅读