Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is only a simple example of schedules
- */
- package beeper;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.ScheduledFuture;
- import static java.util.concurrent.TimeUnit.SECONDS;
- /**
- *
- * @author bernardovieira
- */
- public class Beeper {
- private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Beeper beep = new Beeper();
- beep.beepForAnHour();
- beep.stopIt();
- }
- public void stopIt()
- {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Thread.sleep(2000);
- scheduler.shutdown();
- } catch (InterruptedException ex) { }
- //if you want to start another beeper
- //just uncomment the code below
- /*Beeper beep = new Beeper();
- beep.beepForAnHour();
- beep.stopIt();*/
- }
- }).start();
- }
- public void beepForAnHour()
- {
- final Runnable beeper = new Runnable()
- {
- @Override
- public void run()
- {
- System.out.println("beep");
- }
- };
- scheduler.scheduleAtFixedRate(beeper, 1, 1, SECONDS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement