第5章 定时器Timer

第5章 定时器Timer
标签: Java多线程编程

《Java多线程编程核心技术》 个人笔记

第5章 定时器Timer
定时器Timer的使用
方法scheduleTimerTask task Date time的测试
方法scheduleTimerTask task Date firstTime long period的测试
方法scheduleTimerTask task long delay的测试
方法scheduleTimerTask task long delay long period的测试
方法scheduleAtFixedRateTimerTask task Date firstTime long period的测试
追赶性

本章着重掌握一下技术点:

  1. 如何实现指定时间执行任务
  2. 如何实现按指定周期执行任务
    定时器Timer的使用

在JDK库中Timer类主要负责计划任务的功能,也就是在指定的时间开始执行某一个任务。
Timer类的主要作用就是设置计划任务,但封装任务的类却是TimerTask类

方法schedule(TimerTask task, Date time)的测试

该方法的作用是在指定的日期执行一次某一任务

public class Run1 {
private static Timer timer = new Timer(); //定时器
static public class MyTask extends TimerTask { //任务类br/>@Override
public void run() {
System.out.println("运行了!时间为:" + new Date());
}
}
public static void main(String[] args) {
try {
MyTask task = new MyTask();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = "2014-10-12 11:55:00";

        Date dateRef = sdf.parse(dateString);   //时间
        System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
                + new Date().toLocaleString());
        timer.schedule(task, dateRef);          //做任务
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

}

将定时器设置为守护线程,构造参数传入true:private static Timer timer = new Timer(True);
如果计划时间早于当前时间,则立刻执行task任务
Timer中允许有多个TimerTask任务

public static void main(String[] args) {
try {
MyTask1 task1 = new MyTask1();
MyTask2 task2 = new MyTask2();

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateString1 = "2014-10-12 10:39:00";
        String dateString2 = "2014-10-12 10:40:00";

        Date dateRef1 = sdf1.parse(dateString1);
        Date dateRef2 = sdf2.parse(dateString2);

        System.out.println("字符串1时间:" + dateRef1.toLocaleString() + " 当前时间:"
                + new Date().toLocaleString());
        System.out.println("字符串2时间:" + dateRef2.toLocaleString() + " 当前时间:"
                + new Date().toLocaleString());

        timer.schedule(task1, dateRef1);    //多个任务
        timer.schedule(task2, dateRef2);    //多个任务
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

TimerTask是以队列的方式一个一个被顺序执行的,所以执行的时间有可能和预期的时间不一致,因为前面的任务有可能消耗的时间较长,则后面的任务运行的时间也会被延迟

方法schedule(TimerTask task, Date firstTime, long period)的测试

该方法的作用是在指定的日期之后,按指定的建个周期性地无限循环地执行某一任务

timer.schedule(task, dateRef, 4000); //每隔4秒执行一次

TimerTask类的方法cancel()的作用是将自身从任务队列中清除,其他任务不受影响

static public class MyTaskA extends TimerTask {
    @Override
    public void run() {
        System.out.println("A运行了!时间为:" + new Date());
        this.cancel();
    }
}

和TimerTask类的方法cancel()不同,Timer类中的cancel()方法的作用是将任务队列中的全部任务清空,并且进程被销毁

private static Timer timer = new Timer();
static public class MyTaskA extends TimerTask {br/>@Override
public void run() {
System.out.println("A运行了!时间为:" + new Date());
timer.cancel(); //全部清空
}
}

因为Timer类中的cancel()放油有时并没有强盗queue锁,所以有时并不一定会停止执行计划任务

方法schedule(TimerTask task, long delay)的测试

该方法是以执行schedule(TimerTask task, long delay)方法当前的时间为参考时间,在此时间基础上延迟指定的毫秒数后执行一次TimerTask任务

public static void main(String[] args) {
    MyTask task = new MyTask();
    Timer timer = new Timer();
    System.out.println("当前时间:" + new Date().toLocaleString());
    timer.schedule(task, 7000);
}

方法schedule(TimerTask task, long delay, long period)的测试

该方法的作用是以执行schedule(TimerTask task, long delay, long period)方法当前的时间为参考时间,在此时间基础上延迟指定的毫秒数,再以某一间隔时间无线次数地执行某一任务

public static void main(String[] args) {
        MyTask task = new MyTask();
        Timer timer = new Timer();
        System.out.println("当前时间:"+new Date().toLocaleString());
        timer.schedule(task, 3000,5000);
}

方法scheduleAtFixedRate(TimerTask task, Date firstTime, long period)的测试

使用schedule方法:如果执行任务的时间没有被延时,那么下一次任务的执行时间参考的是上一次任务的“开始”时的时间来计算
使用scheduleAtFixedRate方法:如果执行任务的时间没有被延时,那么下一次任务的执行时间参考的是上一次任务的“结束”的时间来计算
延时则没有区别,都是参考上一次任务“结束”时的时间来计算

追赶性

转载于:https://blog.51cto.com/13545923/2053316

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值