定时执行案例二

 

http://blog.sina.com.cn/s/blog_401a71d1010005cc.html

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

public class ScheduleIterator {
private final int hourOfDay, minute, second;
private final Calendar calendar = Calendar.getInstance();

public ScheduleIterator(int month, int day, int hourOfDay, int minute,
int second) {
this(month, day, hourOfDay, minute, second, new Date());
}

public ScheduleIterator(int month, int day, int hourOfDay, int minute,
int second, Date date) {
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
calendar.setTime(date);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
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.MINUTE, 1);
return calendar.getTime();
}

}

Scheduler.java
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() {
}

/**
* Terminates this <code>Scheduler</code>, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a scheduler has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it.
* <p>
* Note that calling this method from within the run method of a scheduler task that was invoked by this scheduler absolutely guarantees that the ongoing task execution is the last task execution that will ever be performed by this scheduler.
* <p>
* This method may be called repeatedly; the second and subsequent calls have no effect.
*/

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

/**
* Schedules the specified task for execution according to the specified schedule. If times specified by the <code>ScheduleIterator</code> are in the past they are scheduled for immediate execution.
* <p>
* @param schedulerTask task to be scheduled
* @param iterator iterator that describes the schedule
* @throws IllegalStateException if task was already scheduled or cancelled, scheduler was cancelled, or scheduler thread terminated.
*/

public void schedule(SchedulerTask schedulerTask,
ScheduleIterator iterator) {

Date time = iterator.next();
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);
System.out.println("time:s=" + schedulerTask.timerTask + "time:t=" +
time.toLocaleString());
timer.schedule(schedulerTask.timerTask, time);
}
}
}

private void reschedule(SchedulerTask schedulerTask,
ScheduleIterator iterator) {

Date time = iterator.next();
if (time == null) {
schedulerTask.cancel();
}
else {
synchronized (schedulerTask.lock) {
if (schedulerTask.state != SchedulerTask.CANCELLED) {
schedulerTask.timerTask =
new SchedulerTimerTask(schedulerTask, iterator);
System.out.println("time1:s=" + schedulerTask.timerTask + "time1:t=" +
time.toLocaleString());
timer.schedule(schedulerTask.timerTask, time);
}
}
}
}

}

SchedulerTask.java
import java.util.TimerTask;

/**
* A task that can be scheduled for recurring execution by a {@link Scheduler}.
*/
public abstract class SchedulerTask
implements Runnable {

final Object lock = new Object();
int state = VIRGIN;
static final int VIRGIN = 0;
static final int SCHEDULED = 1;
static final int CANCELLED = 2;
TimerTask timerTask;
protected SchedulerTask() {
}

public abstract void run();

public boolean cancel() {
synchronized (lock) {
if (timerTask != null) {
timerTask.cancel();
}
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}

public long scheduledExecutionTime() {
synchronized (lock) {
return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
}
}

}

AlarmClock.java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.*;

public class AlarmClock {
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
private final Scheduler scheduler = new Scheduler();
private final SimpleDateFormat dateFormat =
new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS");
private final int month, day, hourOfDay, minute, second;
private final String content;

public AlarmClock(int month, int day, int hourOfDay, int minute, int second,
String content) {
this.month = month;
this.day = day;
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
this.content = content;
}

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

private void soundAlarm() {
// 在这里放要执行的东西
System.out.println( "我* . 执行到这儿了. "+content);
}
}

, new ScheduleIterator(month, day, hourOfDay, minute, second));
}

public static void main(String[] args) {
AlarmClock alarmClock = new AlarmClock(3, 14, 16, 31, 40,
"hello world !!!");
alarmClock.start();
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值