timer的重要函数详解

来源:互联网 发布:澳门网络博客 编辑:程序博客网 时间:2024/06/05 07:20

timer类
package timer;

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

public class MyTimer extends Timer {
public static void main(String[] args) {
//1.创建一个timer实例
Timer timer = new Timer();
//2.创建一个MyTimerTask实例
MyTimerTask myTimerTask = new MyTimerTask(“Tom”);
//3.通过timer定时定频率调用myTimerTask的业务逻辑
//即第一次执行是在当前时间的两秒后,之后每隔1秒钟执行一次
// timer.schedule(myTimerTask, 2000L, 1000L);

    //获取当前时间,并设置成距离当前时间3秒之后的时间    //如当前时间是2016-11-10 23:59:57    //则设置后的时间是2016-11-11 00:00:00    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);    //----------- schedule的用法  ------------    /**     * 第一种     * schedule(task,time)     * task -----> 所要执行的任务     * time------> 执行任务的市价 (什么时候执行任务)     * 在时间等于或超过time的时候执行 且只执行一次task     * 例如在当前时间的3秒后,打印任务的名字     */    /*MyTimerTask schedule1 = new MyTimerTask("schedule1");    //        timer.schedule(schedule1, calendar.getTime());    timer.schedule(schedule1, calendar.getTime());    System.out.println(calendar.getTime());*/    /**     * 第二种     * schedule(task,time,period)     * task -----> 所要执行的任务     * time------> 执行任务的市价 (什么时候执行任务)     * period 执行一次task的时间间隔,单位是毫秒。     */    /*myTimerTask.setName("schedule2");    timer.schedule(myTimerTask, calendar.getTime(), 2000L);*/    /**     * 第三种     * schedule(task,delay)     * task 所要执行的任务     * delay 执行任务前的延迟时间,单位是毫秒。     * 作用 : delay毫秒后,执行且仅执行一次task     */    /*myTimerTask.setName("schedule3");    timer.schedule(myTimerTask, 3000L);*/    /**     * 第四种     * schedule(task, delay, period)     * task 任务     * delay 延迟时间      * period  时间间隔     */    /*myTimerTask.setName("schedule4");    timer.schedule(myTimerTask, 2000L, 1000L);*/    //---------------  scheduleAtFixedRate(task,time,period)  -------------    //也是timer 下的 函数    /**     * 第一种     * scheduleAtFixedRate(task,time,period)     * task 所要执行的任务     * time 首次执行任务的时间     * period 执行一次task的时间间隔,单位是 毫秒     */    /*myTimerTask.setName("scheduleAtFixedRate");    timer.scheduleAtFixedRate(myTimerTask, calendar.getTime(), 1000L);*/    /**     * 第二种     * scheduleAtFixedRate(task, delay, period)     * task 所要执行的任务     * delay 执行任务钱的延迟时间,单位是 毫秒     * period 执行一次task的 时间间隔,单位是 毫秒     */    /* myTimerTask.setName("schduleAtFixedRate2");     timer.scheduleAtFixedRate(myTimerTask, 2000L, 2000L);*/    /**     * timertask 下的重要函数      * scheduledExecutionTime()函数     * 返回此任务最近实际执行的时间     */    /*myTimerTask.setName("scheduledExecutionTime");    timer.schedule(myTimerTask, 1000L);    System.out.println("当前任务被安排实际执行的时间是:" + sf.format(myTimerTask.scheduledExecutionTime()));*/    /**     * timer下的重要函数     * 1.cancel()函数       * 作用:丢弃所有当前已安排的任务     */    //实例:创建两个新的timertask 任务    MyTimerTask task1 = new MyTimerTask("task1");    MyTimerTask task2 = new MyTimerTask("task2");    //安排 任务task1在当前时间 3秒 执行,每隔2秒执行一次    timer.schedule(task1, 3000L, 2000L);    //安排 任务task2在当前时间 2秒 执行,每隔1秒执行一次    timer.schedule(task2, 2000L, 1000L);    //休眠6秒    try {        Thread.sleep(6000L);    } catch (InterruptedException e) {        e.printStackTrace();    }    //获取当前时间并打印,此时取消所有在执行的任务    Date now = new Date();    System.out.println("取消任务的时间:" + sf.format(now));    //取消所有任务    timer.cancel();    System.out.println("任务task1和task2都取消了");}

}

timertask类
package timer;

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

public class MyTimerTask extends TimerTask {
private Integer count = 0; //计数
private String name;

public MyTimerTask(String taskName) {    name = taskName;}@Overridepublic void run() {    if (count < 10) {        //以yyyy-MM-dd HH:mm:ss的格式打印当前执行的时间        Calendar calendar = Calendar.getInstance();        //格式化时间        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println("当前时间是:" + sf.format(calendar.getTime()));        //打印当前name的内容        System.out.println("我叫" + name);        count++;    } else {        //调用 cancel()函数,取消当前timertask里的任务        testCancel();        System.out.println("执行了cancel()函数任务取消了");    }}public String getName() {    return name;}public void setName(String name) {    this.name = name;}//     --------------- timertask 的其他重要函数 ----------/** * cancel()函数 * 作用:取消当前 timertask 里的任务 */public void testCancel() {    cancel();}

}