一个java定时器框架

ScheduleIterator接口
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif importjava.util.Date;
None.gif
None.gif
public interface ScheduleIterator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
publicDatenext();//获取下一个触发的时间点
ExpandedBlockEnd.gif
}

None.gif

Scheduler

None.gif importjava.util.Date;
None.gifimportjava.util.Timer;
None.gifimportjava.util.TimerTask;
None.gif
None.gif
None.gif
public class Scheduler
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
classSchedulerTimerTaskextendsTimerTask
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
privateSchedulerTaskschedulerTask;
InBlock.gif
privateScheduleIteratoriterator;
InBlock.gif
publicSchedulerTimerTask(SchedulerTaskschedulerTask,ScheduleIteratoriterator)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.schedulerTask=schedulerTask;
InBlock.gif
this.iterator=iterator;
ExpandedSubBlockEnd.gif}

InBlock.gif
publicvoidrun()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifschedulerTask.run();
InBlock.gifreschedule(schedulerTask,iterator);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
privatefinalTimertimer=newTimer();
InBlock.gif
InBlock.gif
publicScheduler()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

InBlock.gif
publicvoidcancel()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giftimer.cancel();
ExpandedSubBlockEnd.gif}

InBlock.gif
publicvoidschedule(SchedulerTaskschedulerTask,ScheduleIteratoriterator)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDatetime
=iterator.next();
InBlock.gif
if(time==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifschedulerTask.cancel();
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsynchronized(schedulerTask.
lock)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(schedulerTask.state!=SchedulerTask.VIRGIN)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
thrownewIllegalStateException("Taskalreadyscheduled"+
InBlock.gif
"orcancelled");
ExpandedSubBlockEnd.gif}

InBlock.gifschedulerTask.state
=SchedulerTask.SCHEDULED;
InBlock.gifschedulerTask.timerTask
=newSchedulerTimerTask(schedulerTask,iterator);
InBlock.giftimer.schedule(schedulerTask.timerTask,time);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
privatevoidreschedule(SchedulerTaskschedulerTask,ScheduleIteratoriterator)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDatetime
=iterator.next();
InBlock.gif
if(time==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifschedulerTask.cancel();
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsynchronized(schedulerTask.
lock)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(schedulerTask.state!=SchedulerTask.CANCELLED)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifschedulerTask.timerTask
=newSchedulerTimerTask(schedulerTask,iterator);
InBlock.giftimer.schedule(schedulerTask.timerTask,time);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

SchedulerTask

None.gif importjava.util.TimerTask;
None.gif
None.gif
public abstract class SchedulerTaskimplementsRunnable
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//被调度器不断调度执行的任务
InBlock.gif

InBlock.giffinalObject
lock=newObject();
InBlock.gif
InBlock.gif
intstate=VIRGIN;//任务状态
InBlock.gif
staticfinalintVIRGIN=0;
InBlock.gif
staticfinalintSCHEDULED=1;
InBlock.gif
staticfinalintCANCELLED=2;
InBlock.gif
InBlock.gifTimerTasktimerTask;
//底层的定时器任务
InBlock.gif
protectedSchedulerTask()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockEnd.gif}

InBlock.gif
publicabstractvoidrun();//调度任务执行的具体任务
InBlock.gif
publicbooleancancel()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsynchronized(
lock)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(timerTask!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giftimerTask.cancel();
//取消任务
ExpandedSubBlockEnd.gif
}

InBlock.gifbooleanresult
=(state==SCHEDULED);//任务已经被调度执行
InBlock.gif
state=CANCELLED;//设置任务状态为“取消”
InBlock.gif
returnresult;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
publiclongscheduledExecutionTime()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsynchronized(
lock)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
returntimerTask==null?0:timerTask.scheduledExecutionTime();//任务执行时间
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

DailyIterator类:

None.gif importjava.util.Calendar;
None.gifimportjava.util.Date;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class DailyIteratorimplementsScheduleIterator dot.gif {
InBlock.gif
privatefinalinthourOfDay,minute,second;
InBlock.gif
privatefinalCalendarcalendar=Calendar.getInstance();
InBlock.gif
InBlock.gif
publicDailyIterator(inthourOfDay,intminute,intsecond)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this(hourOfDay,minute,second,newDate());
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicDailyIterator(inthourOfDay,intminute,intsecond,Datedate)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.hourOfDay=hourOfDay;
InBlock.gif
this.minute=minute;
InBlock.gif
this.second=second;
InBlock.gifcalendar.setTime(date);
InBlock.gifcalendar.
set(Calendar.HOUR_OF_DAY,hourOfDay);
InBlock.gifcalendar.
set(Calendar.MINUTE,minute);
InBlock.gifcalendar.
set(Calendar.SECOND,second);
InBlock.gifcalendar.
set(Calendar.MILLISECOND,0);
InBlock.gif
if(!calendar.getTime().before(date))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcalendar.add(Calendar.DATE,
-1);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicDatenext()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//获取下一个触发的时间点
InBlock.gif
calendar.add(Calendar.DATE,1);
InBlock.gif
returncalendar.getTime();
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif

测试类:

None.gif importjava.text.SimpleDateFormat;
None.gif
None.gifimportjava.util.Date;
None.gif
None.gifimportorg.tiling.scheduling.Scheduler;
None.gifimportorg.tiling.scheduling.SchedulerTask;
None.gifimportorg.tiling.scheduling.examples.iterators.DailyIterator;
None.gif
None.gif
public class AlarmClock
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
privatefinalSchedulerscheduler=newScheduler();//调度器
InBlock.gif
privatefinalSimpleDateFormatdateFormat=
InBlock.gif
newSimpleDateFormat("ddMMMyyyyHH:mm:ss.SSS");
InBlock.gif
privatefinalinthourOfDay,minute,second;//每天触发的时间点
InBlock.gif

InBlock.gif
publicAlarmClock(inthourOfDay,intminute,intsecond)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.hourOfDay=hourOfDay;
InBlock.gif
this.minute=minute;
InBlock.gif
this.second=second;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicvoidstart()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifscheduler.schedule(
newSchedulerTask()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
publicvoidrun()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsoundAlarm();
ExpandedSubBlockEnd.gif}

InBlock.gif
privatevoidsoundAlarm()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.
out.println("Wakeup!"+
InBlock.gif
"It's"+dateFormat.format(newDate()));
InBlock.gif
//Startanewthreadtosoundanalarmdot.gif
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}
,newDailyIterator(hourOfDay,minute,second));//通过迭代器模式迭代遍历得到后面一系列的时间点
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
publicstaticvoidmain(String[]args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifAlarmClockalarmClock
=newAlarmClock(22,5,0);
InBlock.gifalarmClock.start();
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

参考资料

Scheduling recurring tasks in Java applications

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值