Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.time.LocalTime;
- import java.time.temporal.ChronoUnit;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.ScheduledFuture;
- import java.util.concurrent.TimeUnit;
- /**
- *
- * @author zeesousa@github.com
- */
- public class EasyScheduler {
- private final ScheduledExecutorService executor;
- public EasyScheduler() {
- this.executor = Executors.newSingleThreadScheduledExecutor();
- }
- public EasyScheduler(ScheduledExecutorService executor) {
- this.executor = executor;
- }
- public ScheduledFuture<?> scheduleRepeat(Runnable task, LocalTime date) {
- //time from now till our task running date
- long timeToTask = LocalTime.now().until(date, ChronoUnit.SECONDS);
- //run the task at our desired date, repeat every 86400 seconds (24hours)
- return this.executor.scheduleAtFixedRate(task, timeToTask, 86400, TimeUnit.SECONDS);
- }
- public ScheduledExecutorService getExecutor() {
- return executor;
- }
- //put more useful scheduler methods here//
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement