Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ScheduledExecutorService not expose methods to run at a particular time

If I want to schedule at recurring task that is aligned with a specific date it would make sense to use ScheduledExecutorService. But it has no method to pass in the firstRunDate + subsequent delay to a scheduleAtFixedRate method. I know I can back out the initial delay myself but is there any reason this isn't provided by the API? Especially as internally the SES is implemented using triggerTimes (which is what I want to pass in).

like image 406
Mike Q Avatar asked Dec 16 '25 22:12

Mike Q


1 Answers

Based on the documentation:

All schedule methods accept relative delays and periods as arguments, not absolute times or dates. It is a simple matter to transform an absolute time represented as a Date to the required form. For example, to schedule at a certain future date, you can use: schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS). Beware however that expiration of a relative delay need not coincide with the current Date at which the task is enabled due to network time synchronization protocols, clock drift, or other factors.

It looks as if it was a deseign decision. Its widely known that the Date class has its problems. For example TimeTask's public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) does not account for day light savings time.

like image 64
John Vint Avatar answered Dec 19 '25 14:12

John Vint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!