Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3.  
  4. public class TimeExecutor {
  5.  
  6. class LocalSleep extends TimerTask {
  7. private int noOfSeconds;
  8. private int count = 0;
  9. private Timer timer;
  10.  
  11. public LocalSleep(int noOfSeconds, Timer timer) {
  12. this.noOfSeconds = noOfSeconds;
  13. this.timer = timer;
  14. }
  15.  
  16. void sleeeep() {
  17. if (count < noOfSeconds) {
  18. System.out.println("Count: " + count);
  19. count++;
  20. } else
  21. timer.cancel();
  22. }
  23.  
  24. @Override
  25. public void run() {
  26. sleeeep();
  27. }
  28. }
  29.  
  30. public static void main(String args[]) {
  31. Timer timer = new Timer();
  32. TimeExecutor t = new TimeExecutor();
  33. timer.schedule(t.new LocalSleep(5, timer), 1000);
  34. }
  35. }
  36.  
  37. timer.schedule(t.new LocalSleep(5, timer), 1000);
  38.  
  39. timer.schedule(t.new LocalSleep(5, timer),0, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement