@Signature public long scheduledTime() { return getWrappedObject().scheduledExecutionTime(); }
public long scheduledExecutionTime() { synchronized (lock) { return timerTask == null ? 0 : timerTask.scheduledExecutionTime(); } }
public long scheduledExecutionTime() { synchronized (lock) { return timerTask == null ? 0 : timerTask.scheduledExecutionTime(); } }
@Override public long scheduledExecutionTime() { return super.scheduledExecutionTime(); }
@Override public long scheduledExecutionTime() { return super.scheduledExecutionTime(); }
/** * Reschedule previously scheduled task unless it would postpone it. * In other words, if the requested delay is longer than the time remaining * until the previously scheduled execution, the request is ignored. * * @param delay the new delay before the task is to be executed, * measured from the moment the method is invoked. * @return true if successful, false otherwise (that is, if task is already * running or complete) */ public synchronized void setAlarmDelayIfSooner(long delay) { if (delay < 0) { // infinite; can't be more restrictive than anything return; } if (timerTask != null) { long oldDelay = timerTask.scheduledExecutionTime() - System.currentTimeMillis(); if (delay >= oldDelay) { // not more restrictive; ignore request return; } } setAlarmDelay(delay); }
public final class TimerTaskCatcher extends TimerTask { private final TimerTask orig; private final Thread.UncaughtExceptionHandler handler; public TimerTaskCatcher( TimerTask orig, Thread.UncaughtExceptionHandler handler } { if (orig == null || handler == null) { throw new NullPointerException(); } this.orig = orig; this.handler = handler; } @Override public boolean cancel() { return orig.cancel(); } @Override public void run() { try { orig.run(); } catch (Throwable exc) { handler.uncaughtException(Thread.currentThread(), exc); } } @Override public long scheduledExecutionTime() { return orig.scheduledExecutionTime(); } }
public class Y { public static void main(String[] args)throws Exception { Timer timer = new Timer(); Action task = new Action(); timer.schedule( task, 2000, 1000 ); Thread.sleep( 4000 ); // this is where the user request a new interval, 2 sec. task.cancel(); timer.schedule( new Action(), new Date(task.last + 2000), 2000 ); Thread.sleep( 8000 ); task.cancel(); } } class Action extends TimerTask { private long last; public void run(){ last = scheduledExecutionTime(); System.out.println( "tick" ); } public long getLast(){ return last; } }
@Override public void run() { Date actualTime = new Date(); Date scheduledTime = new Date(this.scheduledExecutionTime()); wait_list.add(new Date[] { scheduledTime, actualTime }); if (actualTime.getTime() % 3 == 0) // Add two elements about 33% of the time
long interval = minutes*60*1000; long delay = interval; if(prevTask != null){ delay = System.currentTimeMillis() - prevTask.scheduledExecutionTime(); //time left of previous setting prevTask.cancel(); delay = interval - delay; //difference in time left & new interval if(delay <=0) //if by new setting should've already ran, so run it ASAP... delay = 2000; logger.info(String.format("DB dump was already scheduled before. Set dump delay to %s minutes & setting new schedule to every %s minutes.", delay/60/1000, minutes)); } TimerTask task = new TimerTask(){ private SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss SSS"); private int minutes; public TimerTask initialize(int minutes){ this.minutes = minutes; return this; } public void run() { try { logger.info(String.format("Doing scheduled %s dump to DB. (Configured to occur every %s minutes.)", ft.format(new Date(this.scheduledExecutionTime())), minutes)); dumpToDB(); } catch (NamingException | SQLException e) { e.printStackTrace(); } } }.initialize(minutes); timer.schedule(task, delay, interval); prevTask = task;
/** * Sets new Timer Task that timeouts pending requests. * If task already exists but is too far in the future, it is canceled and new task assigned */ private void scheduleTimeoutRequestsTimer() { PendingRequest nextRequest = getNextTimeoutingPendingRequest(); // Cancel task if (nextRequest == null) { cancelTimeoutPendingRequestTask(); } else { TimerTask task = timeoutPendingRequestsTask.get(); // Task does not exists or is not ok if (task == null || task.scheduledExecutionTime() > nextRequest.timeoutTime) { cancelTimeoutPendingRequestTask(); // Create a new task task = TimerUtil.schedule(timer, timeoutRun, executor, nextRequest.timeoutTime); if (!timeoutPendingRequestsTask.compareAndSet(null, task)) // it was already set task.cancel(); } } }
/** * Sets new Timer Task that timeouts pending requests. * If task already exists but is too far in the future, it is canceled and new task assigned */ private void scheduleTimeoutRequestsTimer() { HttpsClientPendingRequest nextRequest = _getNextTimeoutingPendingRequest(); // Cancel task if (nextRequest == null) { cancelTimeoutPendingRequestTask(); } else { TimerTask task = timeoutPendingRequestsTask.get(); // Task does not exists or is not ok if (task == null || task.scheduledExecutionTime() > nextRequest.timeoutTime) { cancelTimeoutPendingRequestTask(); // Create a new task task = TimerUtil.schedule(timer, timeoutRun, executor, nextRequest.timeoutTime); if (!timeoutPendingRequestsTask.compareAndSet(null, task)) // it was already set task.cancel(); } } }
long timeLeft; if (startTime < 0 || restart) { startTime = scheduledExecutionTime(); timeLeft = totalTime; restart = false; } else { timeLeft = totalTime - (scheduledExecutionTime() - startTime);