RYN RYN - 6 months ago 70
Java Question

How to schedule a periodic task in Java?

I need to schedule a task to run in at fixed interval of time. How can I do this with support of long intervals (for example on each 8 hours)?

I'm currently using

java.util.Timer.scheduleAtFixedRate
. Does
java.util.Timer.scheduleAtFixedRate
support long time intervals?

Answer

Use a ScheduledExecutorService:

 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
 scheduler.scheduleAtFixedRate(yourRunnable, 8, 8, TimeUnit.HOURS);