定时执行调度任务

这里我们用JDK 5.0里的ScheduledExecutorService,它是一个接口,我们所利用的方法为

ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。

initialDelay允许出现 0 和负数延迟(但不是周期),并将这些视为一种立即执行的请求.
下面我举例用来实现一个Task,在每天的上午8点运行.
代码如下:

public class SchedulerSample {

/**
* 定时执行调度类,来定时调度
*/
private ScheduledExecutorService schuledExecutor;

private final static String timer = "08:00:00";
//所有定时任务的Future
private Map<String,Future<?>> futureMap = new Hashtable<String,Future<?>>();

/**
* Construct for SchedulerSample.java.
*/
public SchedulerSample() {

}

//可以创建该schedule的方法,如init start stop reload etc.
void start() throws ParseException{
String result = "Admin's test ";
schuledExecutor = Executors.newScheduledThreadPool(3);
MockTask task = new MockTask(result);
long PERIOD = 24 * 60 * 60 * 1000L;
//TimeUnit.MILLISECONDS 指明时间单位.为毫秒
long delay = 0;
String key = "only a test ";
long curTime = System.currentTimeMillis();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String tmpstr = df.format(new Date(curTime));
String tmpstr2 = tmpstr.substring(0, 11) + timer;
long executeTime = df.parse(tmpstr2).getTime();
//这个值就是自程序运行时,多久以后运行MockTask.
delay = executeTime - curTime < 0 ? (executeTime - curTime + PERIOD) : (executeTime - curTime);
//here for test , assign delay = 0 or delay is negative and PERIOD = 10 * 1000;
delay = 0;
delay = -1;
PERIOD = 10 * 1000;
Future<?> future = schuledExecutor.scheduleAtFixedRate(task, delay , PERIOD, TimeUnit.MILLISECONDS);
futureMap.put(key, future);
}

void stop(){

schuledExecutor.shutdown();
for(Future<?> fu : futureMap.values()){
fu.cancel(true);
}
futureMap.clear();
}
void reload() throws ParseException{
stop();
start();

}
//etc methods,here dont care that

/**
* here is the test
* @throws ParseException
*/
public static void main(String[] args) throws ParseException{
SchedulerSample ss = new SchedulerSample();
ss.start();
}

}
/**
* .
* @author Admin Jul 21, 2008 3:23:38 PM
*
*/
class MockTask implements Runnable{
String print = "";
//here implemtion we only print a few word;
public MockTask(String print){
this.print = print;
}
private void Print(String str){
System.out.print(new Date(System.currentTimeMillis())+" | ");
System.out.println(str);

}
public void run() {
Print(this.print);
}
}


运行结果,Console出现如下

Mon Jul 21 15:36:03 CST 2008 | Admin's test
Mon Jul 21 15:36:13 CST 2008 | Admin's test
Mon Jul 21 15:36:23 CST 2008 | Admin's test
Mon Jul 21 15:36:33 CST 2008 | Admin's test
Mon Jul 21 15:36:43 CST 2008 | Admin's test
Mon Jul 21 15:36:53 CST 2008 | Admin's test
Mon Jul 21 15:37:03 CST 2008 | Admin's test
Mon Jul 21 15:37:13 CST 2008 | Admin's test
Mon Jul 21 15:37:23 CST 2008 | Admin's test
Mon Jul 21 15:37:33 CST 2008 | Admin's test

时间周期就是我们所测试设定的10*1000,其他的时间依次类推..定点执行,可以用delay来计算.具体请参照代码.请大家批评指正.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值