定时执行调度任务

这里我们用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来计算.具体请参照代码.请大家批评指正.

你可能感兴趣的:(jdk,J2SE,sun,idea)