java按指定时间间隔执行任务代码,java间隔任务代码,[Java]代码pack

[Java]代码

package org.apple.timertask;import java.util.Calendar;import java.util.Date;/** * 
*作用是:计算最近一次计划任务的时间和下一次计划任务的时间.好处是可以指定那个时间进行设置 
*/public class TimeCreator { private final Calendar calendar = Calendar.getInstance(); /** 
* 时间间隔方式 */ private int way; private int spaceTime = 0; 
public TimeCreator(int way, int spaceTime) { this.way = way; if (spaceTime <= 0) 
{ 
throw new IllegalArgumentException("参数非法,必须大于0"); 
} this.spaceTime = spaceTime; Date date = new Date(); calendar.setTime(date); 
if (!calendar.getTime().before(date)) { calendar.add(way, -this.spaceTime); } } /** * 
* @param hour * 小时 * @param minute * 分钟 * @param second * 秒 
* @serialData hourOfDay+minute+second = 开始时间 * @see Calendar 日期设定方式 * @param date 
* 计划日期 * @param way * 时间间隔方式,格式是Calendar.DATE,表示以天为单位间隔 ,Calendar.SECOND表示以秒为单位间隔 * 
* @param spaceTime * 每次间隔值 */ public TimeCreator(int hour, int minute, int second, Date date, int way, int spaceTime) { this.way = way; if (spaceTime <= 0) { throw new IllegalArgumentException("参数非法,必须大于0"); } this.spaceTime = spaceTime; // 先设定日期,然后对时分秒进行细节的修改 calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, second); calendar.set(Calendar.MILLISECOND, 0); if (!calendar.getTime().before(date)) { calendar.add(way, -this.spaceTime); } } public TimeCreator(int hour, int minute, int second, int way, int spaceTime) { this(hour, minute, second, new Date(), way, spaceTime); } /** * 生成下一次计划任务的时间.该方法是本工具类的设计的目的 */ public Date next() { calendar.add(this.way, this.spaceTime); return calendar.getTime(); }}package org.apple.timertask;import java.util.TimerTask;/** * 将TimerTask封装到一个线程中. */public abstract class TimerTaskThread implements Runnable { final Object lock = new Object(); static final int WaitingForRun = 0; static final int Scheduled = 1; static final int Cancelled = 2; int state = WaitingForRun; TimerTask timerTask; /** * 无参构造方法需要直接实现run()方法 */ public TimerTaskThread() { super(); } public TimerTaskThread(TimerTask timerTask) { super(); this.timerTask = timerTask; } public boolean cancel() { synchronized (lock) { if (timerTask != null) { timerTask.cancel(); } boolean result = (state == TimerTaskThread.Scheduled); state = TimerTaskThread.Cancelled; return result; } } public abstract void run();}package org.apple.timertask;import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class Scheduler { // 内部类 class SchedulerTimerTask extends TimerTask { private TimerTaskThread timerTaskThread; private TimeCreator timeCreator; // 外界需要传入的参数 public SchedulerTimerTask(TimerTaskThread timerTaskThread, TimeCreator timeCreator) { this.timerTaskThread = timerTaskThread; this.timeCreator = timeCreator; } // 对外提供的方法 public void run() { this.timerTaskThread.run(); reschedule(this.timerTaskThread, this.timeCreator); } } private Timer timer = new Timer(); public void cancel() { timer.cancel(); timer = null; } /** * 再次执行定时计划 */ private void reschedule(TimerTaskThread schedulerTask, TimeCreator iterator) { Date time = iterator.next(); if (time == null) { schedulerTask.cancel(); } else { synchronized (schedulerTask.lock) { if (schedulerTask.state != TimerTaskThread.Cancelled) { schedulerTask.timerTask = new SchedulerTimerTask(schedulerTask, iterator); if (timer != null) { timer.schedule(schedulerTask.timerTask, time); } else { // 计划任务完成 } } } } } /** * 启动计划任务 */ public void schedule(TimerTaskThread schedulerTask, TimeCreator timeCreator) { Date time = timeCreator.next(); if (time == null) { schedulerTask.cancel(); } else { synchronized (schedulerTask.lock) { if (schedulerTask.state != TimerTaskThread.WaitingForRun) { throw new IllegalStateException("Task already scheduled or cancelled"); } schedulerTask.state = TimerTaskThread.Scheduled; schedulerTask.timerTask = new SchedulerTimerTask(schedulerTask, timeCreator); if (timer != null) { // timer.[public void schedule(TimerTask task, Date time)] // 核心:执行SchedulerTimerTask的run()方法. timer.schedule(schedulerTask.timerTask, time); } else { // 计划任务完成 } } } }}package org.apple.timertask;import java.util.Calendar;/** * 计划任务用例。 */public class TimerTaskTest { public static void main(String[] args) { // 指定时间开始进行计划任务 TimerTaskTest test = new TimerTaskTest(10, 39, 0, Calendar.SECOND, 10); // 2012-11-21 10-36 test.test(); } private final int hour, minute, second, type, spaceTime; public TimerTaskTest(int hour, int minute, int second, int type, int spaceTime) { this.hour = hour; this.minute = minute; this.second = second; this.type = type; this.spaceTime = spaceTime; } public void test() { final Scheduler scheduler = new Scheduler(); scheduler.schedule(new TimerTaskThread() { @Override public void run() { try { testTimerTask(); // scheduler.cancel();进行条件判断 if (1 == 1) { // scheduler.cancel(); } } catch (Exception e) { e.printStackTrace(); } } private void testTimerTask() { System.out.println("test"); } }, new TimeCreator(this.hour, this.minute, this.second, this.type, this.spaceTime)); }}