Is there a way to determine the current millisecond or other time measure of when a ScheduledExecutorService is going to fire next?
scheduleTaskExecutorUpdate = Executors.newSingleThreadScheduledExecutor();
I have a longer running ScheduledExecutorService(A) and from a shorter running ScheduledExecutorService(B) I would like to update a TextView, display a countdown of when ScheduledExecutorService(A) is going to fire next.
If you keep track of the
ScheduledFutures for all tasks scheduled with the executor, then yes. This becomes a problem of determining the minimum delay until the next task must fire, which should be a fairly reliable estimate.… or, for one task, you merely do:
Now, if you’re going to be doing it a lot, with mutiple tasks, I’d suggest you maintain a
DelayQueue. However, you can’t merely throw theScheduledFutures in the queue without maintaining the changes caused by periodic tasks. Luckily, the classScheduledThreadPoolExecutorshould handle this nicely via itsdecorateTaskmethods.Note this means you will need to create your own
ScheduledThreadPoolExecutordirectly. Something like the below might work.Then, usage is as follows.