Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.noway;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- public class MinuteMan implements Runnable {
- private boolean killThread = false;
- @Override
- public void run() {
- while (!killThread) {
- if (Thread.currentThread().isInterrupted()) {
- System.out.println("Shutdown Thread isInterrupted Signal detected. "+Thread.currentThread().getName());
- killThread = true;
- break;
- }
- Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
- Date date = new Date(); // given date
- calendar.setTime(date); // assigns calendar to given date
- int minuteNow = calendar.get(Calendar.MINUTE);
- while (minuteNow == getMinuteFromTime() && !killThread) {
- if (Thread.currentThread().isInterrupted()) {
- System.out.println("Shutdown Thread isInterrupted Signal detected. "+Thread.currentThread().getName());
- killThread = true;
- Thread.currentThread().interrupt();
- break;
- }
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- //Fire again to kill thread.
- Thread.currentThread().interrupt();
- }
- }
- if (!killThread) {
- //Fire Your Actions Here
- System.out.println("Fire ACTION minute has changed from: " + minuteNow + " to " + getMinuteFromTime());
- minuteNow = getMinuteFromTime();
- System.out.println(">>> Number of Active Threads >>> "+ Thread.activeCount() +" at minute " + minuteNow;
- }
- }
- System.out.println("MinuteMan Thread Dead: "+Thread.currentThread().getName());
- }
- public boolean isKillThread() {
- return killThread;
- }
- public void setKillThread(boolean killThread) {
- this.killThread = killThread;
- }
- private int getMinuteFromTime() {
- Calendar cal = GregorianCalendar.getInstance(); // creates a new calendar instance
- Date d = new Date(); // given date
- cal.setTime(d); // assigns calendar to given date
- int newMin = cal.get(Calendar.MINUTE); // gets hour in 24h format
- return newMin;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement