开发者

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).


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.


java.util.Timer provides this with TimerTask as it has a method

public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

but you lose the thread pooling and Future support of ExecutorService.

Edit: To answer your original question I guess it just didn't occur to the author to overload the method with a date.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜