一.Timer
举个简单例子。每隔5秒自动刷新。
Timer timerFresh = new Timer(); timerFresh.schedule(new TimerTask() { public void run() { Display.getDefault().syncExec(new Runnable() { public void run() { setInputValue(); } }); } }, 5000 , 5000 );
二.ScheduledExecutorService
ScheduledExecutorService schedule(Runnablecommand, long delay, TimeUnitunit) : ScheduledFuture schedule(Callable<V> callable, long delay, TimeUnitunit) : ScheduledFuture scheduleAtFixedRate(Runnablecomand, long initDelay, long period, TimeUnitunit) : ScheduledFuture scheduleWithFixedDelay(Runnablecommand, long initDelay, long delay, TimeUnitunit) :
ScheduledFuturejava.util.concurrent.Executors是ScheduledExecutorService的工厂类,通过Executors,你可以创建你所需要的ScheduledExecutorService。JDK 1.5之后有了ScheduledExecutorService,不建议你再使用java.util.Timer,因为它无论功能性能都不如ScheduledExecutorService。 ScheduledExecutorService ScheduledTaskSubmitter ScheduleFuture<Object> future = scheduler.schedule(task, 1, TimeUnit.SECONDS); // 等待到任务被执行完毕返回结果 // 如果任务执行出错,这里会抛ExecutionException future.get(); //取消调度任务 future.cancel();
ScheduledFuturejava.util.concurrent.Executors是ScheduledExecutorService的工厂类,通过Executors,你可以创建你所需要的ScheduledExecutorService。JDK 1.5之后有了ScheduledExecutorService,不建议你再使用java.util.Timer,因为它无论功能性能都不如ScheduledExecutorService。
比如这篇文章讲的很好。
http://sunnylocus.javaeye.com/blog/530969
三.quartz
这个目前考虑的比较全面用的比较多。
package org.quartz.examples.example4; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.SimpleTrigger; import org.quartz.TriggerUtils; import org.quartz.impl.StdSchedulerFactory; public class JobStateExample { public void run() throws Exception { Log log = LogFactory.getLog(JobStateExample.class ); log.info("------- Initializing -------------------" ); SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------" ); log.info("------- Scheduling Jobs ----------------" ); long ts = TriggerUtils.getNextGivenSecondDate( null , 10 ).getTime(); JobDetail job1 = new JobDetail( "job1" , "group1" , ColorJob. class ); SimpleTrigger trigger1 = new SimpleTrigger( "trigger1" , "group1" , "job1" , "group1" , new Date(ts), null , 4 , 10000 ); job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Green" ); job1.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1 ); Date scheduleTime1 = sched.scheduleJob(job1, trigger1); log.info(job1.getFullName() + " will run at: " + scheduleTime1 + " and repeat: " + trigger1.getRepeatCount() + " times, every " + trigger1.getRepeatInterval() / 1000 + " seconds" ); JobDetail job2 = new JobDetail( "job2" , "group1" , ColorJob. class ); SimpleTrigger trigger2 = new SimpleTrigger( "trigger2" , "group1" , "job2" , "group1" , new Date(ts + 1000 ), null , 4 , 10000 ); job2.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Red" ); job2.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1 ); Date scheduleTime2 = sched.scheduleJob(job2, trigger2); log.info(job2.getFullName() + " will run at: " + scheduleTime2 + " and repeat: " + trigger2.getRepeatCount() + " times, every " + trigger2.getRepeatInterval() / 1000 + " seconds" ); log.info("------- Starting Scheduler ----------------" ); sched.start(); log.info("------- Started Scheduler -----------------" ); log.info("------- Waiting 60 seconds... -------------" ); try { Thread.sleep(60L * 1000L); } catch (Exception e) { } log.info("------- Shutting Down ---------------------" ); sched.shutdown(true ); log.info("------- Shutdown Complete -----------------" ); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.numJobsExecuted() + " jobs." ); } public static void main(String[] args) throws Exception { JobStateExample example = new JobStateExample(); example.run(); } }