一个java定时器框架

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

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

None.gif

Scheduler

None.gif import java.util.Date;
None.gifimport java.util.Timer;
None.gifimport java.util.TimerTask;
None.gif
None.gif
None.gif
public   class  Scheduler 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class SchedulerTimerTask extends TimerTask 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private SchedulerTask schedulerTask;
InBlock.gif        
private ScheduleIterator iterator;
InBlock.gif        
public SchedulerTimerTask(SchedulerTask schedulerTask,ScheduleIterator iterator)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.schedulerTask = schedulerTask;
InBlock.gif            
this.iterator = iterator;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void run() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            schedulerTask.run();
InBlock.gif            reschedule(schedulerTask, iterator);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private final Timer timer = new Timer();
InBlock.gif
InBlock.gif    
public Scheduler() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public void cancel() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        timer.cancel();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public void schedule(SchedulerTask schedulerTask,ScheduleIterator iterator) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Date time 
= iterator.next();
InBlock.gif        
if (time == null
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            schedulerTask.cancel();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            synchronized(schedulerTask.
lock)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (schedulerTask.state != SchedulerTask.VIRGIN) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new IllegalStateException("Task already scheduled " +
InBlock.gif                        
"or cancelled");
ExpandedSubBlockEnd.gif                }

InBlock.gif                schedulerTask.state 
= SchedulerTask.SCHEDULED;
InBlock.gif                schedulerTask.timerTask 
= new SchedulerTimerTask(schedulerTask, iterator);
InBlock.gif                timer.schedule(schedulerTask.timerTask, time);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private void reschedule(SchedulerTask schedulerTask,ScheduleIterator iterator)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Date time 
= iterator.next();
InBlock.gif        
if (time == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            schedulerTask.cancel();
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            synchronized(schedulerTask.
lock)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (schedulerTask.state != SchedulerTask.CANCELLED) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    schedulerTask.timerTask 
= new SchedulerTimerTask(schedulerTask, iterator);
InBlock.gif                    timer.schedule(schedulerTask.timerTask, time);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

SchedulerTask

None.gif import java.util.TimerTask;
None.gif
None.gif
public   abstract   class  SchedulerTask implements Runnable 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//被调度器不断调度执行的任务
InBlock.gif

InBlock.gif    final Object 
lock = new Object();
InBlock.gif
InBlock.gif    
int state = VIRGIN;//任务状态
InBlock.gif
    static final int VIRGIN = 0;
InBlock.gif    
static final int SCHEDULED = 1;
InBlock.gif    
static final int CANCELLED = 2;
InBlock.gif
InBlock.gif    TimerTask timerTask;
//底层的定时器任务
InBlock.gif
    protected SchedulerTask() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public abstract void run();//调度任务执行的具体任务
InBlock.gif
    public boolean cancel() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        synchronized(
lock
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (timerTask != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                timerTask.cancel();
//取消任务
ExpandedSubBlockEnd.gif
            }

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

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public long scheduledExecutionTime()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        synchronized(
lock
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return timerTask == null ? 0 : timerTask.scheduledExecutionTime();//任务执行时间
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

DailyIterator类:

None.gif import java.util.Calendar;
None.gifimport java.util.Date;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  DailyIterator implements ScheduleIterator  dot.gif {
InBlock.gif    
private final int hourOfDay, minute, second;
InBlock.gif    
private final Calendar calendar = Calendar.getInstance();
InBlock.gif
InBlock.gif    
public DailyIterator(int hourOfDay, int minute, int second)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this(hourOfDay, minute, second, new Date());
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public DailyIterator(int hourOfDay, int minute, int second, Date date)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.hourOfDay = hourOfDay;
InBlock.gif        
this.minute = minute;
InBlock.gif        
this.second = second;
InBlock.gif        calendar.setTime(date);
InBlock.gif        calendar.
set(Calendar.HOUR_OF_DAY, hourOfDay);
InBlock.gif        calendar.
set(Calendar.MINUTE, minute);
InBlock.gif        calendar.
set(Calendar.SECOND, second);
InBlock.gif        calendar.
set(Calendar.MILLISECOND, 0);
InBlock.gif        
if (!calendar.getTime().before(date)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            calendar.add(Calendar.DATE, 
-1);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

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

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif

测试类:

None.gif import java.text.SimpleDateFormat;
None.gif
None.gifimport java.util.Date;
None.gif
None.gifimport org.tiling.scheduling.Scheduler;
None.gifimport org.tiling.scheduling.SchedulerTask;
None.gifimport org.tiling.scheduling.examples.iterators.DailyIterator;
None.gif
None.gif
public   class  AlarmClock 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
private final Scheduler scheduler = new Scheduler();//调度器
InBlock.gif
    private final SimpleDateFormat dateFormat =
InBlock.gif        
new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS");
InBlock.gif    
private final int hourOfDay, minute, second;//每天触发的时间点 
InBlock.gif

InBlock.gif    
public AlarmClock(int hourOfDay, int minute, int second) 
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    
public void start() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        scheduler.schedule(
new SchedulerTask() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public void run() 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                soundAlarm();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
private void soundAlarm()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.
out.println("Wake up! " +
InBlock.gif                    
"It's " + dateFormat.format(new Date()));
InBlock.gif                
// Start a new thread to sound an alarmdot.gif
ExpandedSubBlockEnd.gif
            }

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

InBlock.gif
InBlock.gif    
public static void main(String[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        AlarmClock alarmClock 
= new AlarmClock(2250);
InBlock.gif        alarmClock.start();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

参考资料

Scheduling recurring tasks in Java applications

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值