timer demo 之二

阅读更多

1.先写一个接口

interface ScheduleIterator.java

import java.util.Date;

public interface ScheduleIterator {
public Date next();
public Date next_time();

}

2.实现 class

DailyIterator.java


import java.util.Calendar;
import java.util.Date;

public class DailyIterator implements ScheduleIterator {
private final int hourOfDay, minute, second;

private final Calendar calendar = Calendar.getInstance();

public DailyIterator(int hourOfDay, int minute, int second) {
this(hourOfDay, minute, second, new Date());
}

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

//下一天,

public Date next() {
calendar.add(Calendar.DATE, 1);
return calendar.getTime();
}

//下一个分钟

public Date next_time() {
calendar.add(Calendar.MINUTE, 1);
System.out.println("===="+calendar.getTime());
return calendar.getTime();
}

//内部测试
public static void main(String[] args)
{
DailyIterator d= new DailyIterator(11,45,0);
System.out.println(d.next_time());
}

}

3.Scheduler.java 具体实现的定时任务的class

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Scheduler {

class SchedulerTimerTask extends TimerTask {
private SchedulerTask schedulerTask;

private ScheduleIterator iterator;

public SchedulerTimerTask(SchedulerTask schedulerTask,
ScheduleIterator iterator) {
this.schedulerTask = schedulerTask;
this.iterator = iterator;
}

public void run() {
schedulerTask.run();
reschedule(schedulerTask, iterator);
}
}




private final Timer timer = new Timer();

public Scheduler() {
}

public void cancel() {
timer.cancel();
}

public void schedule(SchedulerTask schedulerTask,
ScheduleIterator iterator) {

Date time = iterator.next_time();
if (time == null) {
schedulerTask.cancel();
} else {
synchronized(schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.VIRGIN) {
throw new IllegalStateException("Task already scheduled " + "or cancelled");
}
schedulerTask.state = SchedulerTask.SCHEDULED;
schedulerTask.timerTask =
new SchedulerTimerTask(schedulerTask, iterator);
timer.schedule(schedulerTask.timerTask, time);
}
}
}

private void reschedule(SchedulerTask schedulerTask,
ScheduleIterator iterator) {

Date time = iterator.next_time();
if (time == null) {
schedulerTask.cancel();
} else {
synchronized (schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.CANCELLED) {
schedulerTask.timerTask = new SchedulerTimerTask(
schedulerTask, iterator);
timer.schedule(schedulerTask.timerTask, time);
}
}
}
}

}

4.AlarmClock.java

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

public class AlarmClock {

private final Scheduler scheduler = new Scheduler();

private final SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd MMM yyyy HH:mm:ss.SSS");

private final int hourOfDay, minute, second;

public AlarmClock(int hourOfDay, int minute, int second) {
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
}

public void start() {
scheduler.schedule(new SchedulerTask() {
public void run() {
soundAlarm();
}

private void soundAlarm() {
System.out.println("Wake up! " + "It's "
+ dateFormat.format(new Date()));
// Start a new thread to sound an alarm...
}
}, new DailyIterator(hourOfDay, minute, second));
}

public static void main(String[] args) {

//13 28 0 从某一个小时分钟秒从始,
AlarmClock alarmClock = new AlarmClock(13, 28, 0);
alarmClock.start();
}
}

你可能感兴趣的:(Java,UP,thread)