我想每天早上5點(diǎn)執(zhí)行某項(xiàng)任務(wù)。因此,我決定使用ScheduledExecutorService此功能,但到目前為止,我已經(jīng)看到了一些示例,這些示例顯示了如何每隔幾分鐘運(yùn)行一次任務(wù)。而且我找不到任何示例來(lái)說(shuō)明如何每天在特定時(shí)間(上午5點(diǎn))每天運(yùn)行任務(wù),并且還考慮了夏令時(shí)的事實(shí)-以下是我的代碼,每15分鐘運(yùn)行一次-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考慮兼顧夏時(shí)制的事實(shí),安排一個(gè)任務(wù)在每天的凌晨5點(diǎn)運(yùn)行?而且TimerTask對(duì)于這個(gè)還是更好ScheduledExecutorService?
如何使用ScheduledExecutorService每天在特定時(shí)間運(yùn)行某些任務(wù)?
慕尼黑8549860
2019-11-11 13:17:31