如果您在战争中需要简单的调度程序(使用 spring 框架),您还可以执行以下操作:
(在 Spring 中,“@PostConstruct”将初始化调度程序 - 所以不需要 main 方法)
@Component
public class Scheduler {
private static final Logger LOG = LoggerFactory.getLogger(Scheduler.class);
@PostConstruct
private void initializeTenSecSchedule() {
final List<Runnable> jobs = new ArrayList<Runnable>();
jobs.add(doSomeTestLogs());
jobs.add(doSomeTestLogs2());
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(jobs.size());
for(Runnable job : jobs){
scheduler.scheduleWithFixedDelay(job, 10, 10, SECONDS);
}
}
/**
* ---------------------some schedule tasks--------------------------
*/
private Runnable doSomeTestLogs(){
final Runnable job = new Runnable() {
public void run() {
LOG.debug("== foo SCHEDULE a", 1);
System.out.println("Method executed at every 10 seconds. Current time is :: "+ new Date());
}
};
return job;
}
private Runnable doSomeTestLogs2(){
final Runnable job = new Runnable() {
public void run() {
LOG.debug("== foo SCHEDULE b", 1);
System.out.println("Method executed at every 10 seconds. Current time is :: "+ new Date());
}
};
return job;
}
}