如果您需要在戰爭中簡單的調度器(帶彈簧框架),你也可以做這樣的事情:
(Spring中的「@PostConstruct」將初始化調度 - 所以沒有必要對主要方法)
@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;
}
}