java 定时器timer_Java基础--定时任务Timer

packagecom.snow.web.util;importjava.io.FileInputStream;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.Timer;importjava.util.TimerTask;public classTimerDemo {public static voidmain(String[] args) {//timer1();

timer2();//timer3();//timer4();

}//第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)

public static voidtimer1() {

Timer timer= newTimer();

timer.schedule(newTimerTask() {public voidrun() {

System.out.println("-------设定要指定任务--------");

}

},2000);//设定指定的时间time,此处为2000毫秒

}//第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行//schedule(TimerTask task, long delay, long period)

public static voidtimer2() {

Timer timer= newTimer();

timer.schedule(newTimerTask() {public voidrun() {

System.out.println("-------设定要指定任务--------");

}

},1000, 1000);

}//第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。//scheduleAtFixedRate(TimerTask task, long delay, long period)

public static voidtimer3() {

Timer timer= newTimer();

timer.scheduleAtFixedRate(newTimerTask() {public voidrun() {

System.out.println("-------设定要指定任务--------");

}

},1000, 2000);

}//第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.//Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)

public static voidtimer4() {

Calendar calendar=Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY,12); //控制时

calendar.set(Calendar.MINUTE, 0); //控制分

calendar.set(Calendar.SECOND, 0); //控制秒

Date time= calendar.getTime(); //得出执行任务的时间,此处为今天的12:00:00

Timer timer= newTimer();

timer.scheduleAtFixedRate(newTimerTask() {public voidrun() {

System.out.println("-------设定要指定任务--------");

}

}, time,1000 * 60 * 60 * 24);//这里设定将延时每天固定执行

}

}

一、Timer介绍

java.util.Timer

java.util.TimerTask

Timer是一个定时器类,通过该类可以为指定的定时任务进行配置。TimerTask类是一个定时任务类,该类实现了Runnable接口,而且是一个抽象类,如下所示:

public abstract class TimerTask implements Runnable

可以通过继承该类,来实现自己的定时任务。

Timer定时器实例有多种构造方法:

Timer()

创建一个新计时器。

Timer(boolean isDaemon)

创建一个新计时器,可以指定其相关的线程作为守护程序运行。

Timer(String name)

创建一个新计时器,其相关的线程具有指定的名称。

Timer(String name, boolean isDaemon)

创建一个新计时器,其相关的线程具有指定的名称,并且可以指定作为守护程序运行。

二、Timer方法

定时执行方法

1、在特定时间执行任务,只执行一次

public void schedule(TimerTask task,Date time)

2、在特定时间之后执行任务,只执行一次

public void schedule(TimerTask task,long delay)

3、指定第一次执行的时间,然后按照间隔时间,重复执行

public void schedule(TimerTask task,Date firstTime,long period)

4、在特定延迟之后第一次执行,然后按照间隔时间,重复执行

public void schedule(TimerTask task,long delay,long period)

参数:

delay: 延迟执行的毫秒数,即在delay毫秒之后第一次执行

period:重复执行的时间间隔

5、第一次执行之后,特定频率执行,与3同

public void scheduleAtFixedRate(TimerTask task,Date firstTime,long period)

6、在delay毫秒之后第一次执行,后按照特定频率执行

public void scheduleAtFixedRate(TimerTask task,long delay,long period)

方法名称schedule()和scheduleAtFixedRate()两者的区别

<1>schedule()方法更注重保持间隔时间的稳定:保障每隔period时间可调用一次

<2>scheduleAtFixedRate()方法更注重保持执行频率的稳定:保障多次调用的频率趋近于period时间,如果任务执行时间大于period,会在任务执行之后马上执行下一次任务

Timer注销

timer.cancel();

三、案例

1、特定时间后执行

public void schedule(TimerTask task,long delay)

参数:

task为:执行任务

delay:时间毫秒数

方法的含义:

在delay毫秒后执行任务task,只执行一次。

案例:

1分钟后同步数据。

同步任务:

packagecom.yank.framework.common;importjava.util.TimerTask;public class SynchroTimerTask extendsTimerTask {

@Overridepublic voidrun() {//TODO Auto-generated method stub

System.out.println("Synchro data to other servers.");

}

}

定时任务:

packagecom.yank.framework.common;importjava.util.Timer;importjava.util.TimerTask;public classSynchroTest {public static voidmain(String[] args) {//TODO Auto-generated method stub

TimerTask task= newSynchroTimerTask();

Timer timer= newTimer();

timer.schedule(task,1000);

}

}

2、案例2:按点吃饭

首先定义吃饭的任务,制定饭点,没小时进行检查,到点就吃饭。

packagecom.yank.framework.common;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.List;importjava.util.TimerTask;/** 定时吃饭

**/

public class EatTimerTask extendsTimerTask {//吃饭时间

private static ListeatTimes;/** 静态初始化

**/

static{

initEatTimes();

}/** 初始化吃饭时间

**/

private static voidinitEatTimes(){

eatTimes= new ArrayList();

eatTimes.add(8);

eatTimes.add(12);

eatTimes.add(18);

}/** 执行

**/@Overridepublic voidrun() {//TODO Auto-generated method stub

Calendar calendar =Calendar.getInstance();

System.out.println("检查是否到了吃饭的点");int hour =calendar.get(Calendar.HOUR_OF_DAY);if(eatTimes.contains(hour))

{

System.out.println("饿了,吃饭...");

}

}

}

定时检查执行:

packagecom.yank.framework.common;importjava.util.Calendar;importjava.util.Date;importjava.util.Timer;importjava.util.TimerTask;public classEatTimerTaskTest {public static voidmain(String[] arg){

TimerTask task= newEatTimerTask();

Calendar calendar=Calendar.getInstance();

Date firstTime=calendar.getTime();//间隔:1小时

long period = 1000 * 60 * 60;//测试时间每分钟一次//period = 1000 * 60;

Timer timer= newTimer();

timer.schedule(task, firstTime, period);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值