Suppose I want to start a thread at a certain time, at the time I want, how can I set the time in the lower code so that I can start a thread at a certain time and after the same thread will support execution after a given time interval. (in the code example, suppose I want to run a beeper at midnight, how can I do this?
class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beepForAnHour() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 60 * 60, SECONDS); } }
Thanks in advance.
source share