Advertisement
Guest User

MinuteMan PAste

a guest
Mar 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. package com.noway;
  2.  
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.GregorianCalendar;
  6.  
  7. public class MinuteMan implements Runnable {
  8.  
  9.  
  10. private boolean killThread = false;
  11.  
  12.  
  13.  
  14. @Override
  15. public void run() {
  16.  
  17.  
  18.  
  19. while (!killThread) {
  20.  
  21. if (Thread.currentThread().isInterrupted()) {
  22. System.out.println("Shutdown Thread isInterrupted Signal detected. "+Thread.currentThread().getName());
  23. killThread = true;
  24. break;
  25. }
  26.  
  27. Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
  28. Date date = new Date(); // given date
  29. calendar.setTime(date); // assigns calendar to given date
  30. int minuteNow = calendar.get(Calendar.MINUTE);
  31.  
  32.  
  33. while (minuteNow == getMinuteFromTime() && !killThread) {
  34. if (Thread.currentThread().isInterrupted()) {
  35. System.out.println("Shutdown Thread isInterrupted Signal detected. "+Thread.currentThread().getName());
  36. killThread = true;
  37. Thread.currentThread().interrupt();
  38. break;
  39. }
  40. try {
  41. Thread.sleep(1000);
  42. } catch (InterruptedException e) {
  43. //Fire again to kill thread.
  44. Thread.currentThread().interrupt();
  45. }
  46.  
  47. }
  48.  
  49. if (!killThread) {
  50. //Fire Your Actions Here
  51. System.out.println("Fire ACTION minute has changed from: " + minuteNow + " to " + getMinuteFromTime());
  52. minuteNow = getMinuteFromTime();
  53. System.out.println(">>> Number of Active Threads >>> "+ Thread.activeCount() +" at minute " + minuteNow;
  54. }
  55.  
  56. }
  57.  
  58.  
  59. System.out.println("MinuteMan Thread Dead: "+Thread.currentThread().getName());
  60. }
  61.  
  62. public boolean isKillThread() {
  63. return killThread;
  64. }
  65.  
  66. public void setKillThread(boolean killThread) {
  67. this.killThread = killThread;
  68. }
  69.  
  70. private int getMinuteFromTime() {
  71. Calendar cal = GregorianCalendar.getInstance(); // creates a new calendar instance
  72. Date d = new Date(); // given date
  73. cal.setTime(d); // assigns calendar to given date
  74. int newMin = cal.get(Calendar.MINUTE); // gets hour in 24h format
  75. return newMin;
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement