我想每天早上5点执行某项任务。因此,我决定使用ScheduledExecutorService此功能,但到目前为止,我已经看到了一些示例,这些示例显示了如何每隔几分钟运行一次任务。而且我找不到任何示例来说明如何每天在特定时间(上午5点)每天运行任务,并且还考虑了夏令时的事实-以下是我的代码,每15分钟运行一次-public class ScheduledTaskExample { private final ScheduledExecutorService scheduler = Executors .newScheduledThreadPool(1); public void startScheduleTask() { /** * not using the taskHandle returned here, but it can be used to cancel * the task, or check if it's done (for recurring tasks, that's not * going to be very useful) */ final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate( new Runnable() { public void run() { try { getDataFromDatabase(); }catch(Exception ex) { ex.printStackTrace(); //or loggger would be better } } }, 0, 15, TimeUnit.MINUTES); } private void getDataFromDatabase() { System.out.println("getting data..."); } public static void main(String[] args) { ScheduledTaskExample ste = new ScheduledTaskExample(); ste.startScheduleTask(); }}有什么办法,我可以ScheduledExecutorService考虑兼顾夏时制的事实,安排一个任务在每天的凌晨5点运行?而且TimerTask对于这个还是更好ScheduledExecutorService?
添加回答
举报
0/150
提交
取消