Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. package test;
  2.  
  3. import java.time.LocalTime;
  4. import java.time.temporal.ChronoUnit;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.ScheduledExecutorService;
  7. import java.util.concurrent.ScheduledFuture;
  8. import java.util.concurrent.TimeUnit;
  9.  
  10. /**
  11. *
  12. * @author zeesousa@github.com
  13. */
  14. public class EasyScheduler {
  15.  
  16. private final ScheduledExecutorService executor;
  17.  
  18. public EasyScheduler() {
  19. this.executor = Executors.newSingleThreadScheduledExecutor();
  20. }
  21.  
  22. public EasyScheduler(ScheduledExecutorService executor) {
  23. this.executor = executor;
  24. }
  25.  
  26. public ScheduledFuture<?> scheduleRepeat(Runnable task, LocalTime date) {
  27. //time from now till our task running date
  28. long timeToTask = LocalTime.now().until(date, ChronoUnit.SECONDS);
  29. //run the task at our desired date, repeat every 86400 seconds (24hours)
  30. return this.executor.scheduleAtFixedRate(task, timeToTask, 86400, TimeUnit.SECONDS);
  31. }
  32.  
  33. public ScheduledExecutorService getExecutor() {
  34. return executor;
  35. }
  36.  
  37. //put more useful scheduler methods here//
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement