/** * Remove the head task from the priority queue. */ void removeMin() { queue[1] = queue[size]; queue[size--] = null; // Drop extra reference to prevent memory leak fixDown(1); }
for (int i = queue.size(); i > 0; i--) { if (queue.get(i).state == TimerTask.CANCELLED) { queue.quickRemove(i); result++; queue.heapify();
while (queue.isEmpty() && newTasksMayBeScheduled) queue.wait(); if (queue.isEmpty()) break; // Queue is empty and will forever remain; die task= queue.getMin(); synchronized (task.lock) queue.removeMin(); continue; // No action required, poll queue again queue.removeMin(); task.state= TimerTask.EXECUTED; queue.rescheduleMin(task.period < 0 ? currentTime - task.period : executionTime + task.period);
@Override public void run() { try { mainLoop(1); } finally { synchronized (queue) { if (!queue.isEmpty()) { long next = queue.getMin().nextExecutionTime; long now = System.currentTimeMillis(); int delta = (int) (next - now); notifyQueue(delta); } } } // try { // mainLoop(0); // } finally { // // Someone killed this Thread, behave as if Timer cancelled // synchronized(queue) { // newTasksMayBeScheduled = false; // queue.clear(); // Eliminate obsolete references // } // } }
/** * Terminates this timer, discarding any currently scheduled tasks. * Does not interfere with a currently executing task (if it exists). * Once a timer has been terminated, its execution thread terminates * gracefully, and no more tasks may be scheduled on it. * * <p>Note that calling this method from within the run method of a * timer task that was invoked by this timer absolutely guarantees that * the ongoing task execution is the last task execution that will ever * be performed by this timer. * * <p>This method may be called repeatedly; the second and subsequent * calls have no effect. */ public void cancel() { synchronized (queue) { thread.newTasksMayBeScheduled= false; queue.clear(); queue.notify(); // In case queue was already empty. } }
synchronized(queue) { while (queue.isEmpty() && newTasksMayBeScheduled) break; if (queue.isEmpty()) break; // Queue is empty and will forever remain; die task = queue.getMin(); synchronized(task.lock) { if (task.state == TimerTask.CANCELLED) { queue.removeMin(); continue; // No action required, poll queue again if (taskFired = (executionTime<=currentTime)) { if (task.period == 0) { // Non-repeating, remove queue.removeMin(); task.state = TimerTask.EXECUTED; } else { // Repeating task, reschedule queue.rescheduleMin( task.period<0 ? currentTime - task.period : executionTime + task.period);
@Override public void run() { try { mainLoop(1); } finally { synchronized (queue) { if (!queue.isEmpty()) { long next = queue.getMin().nextExecutionTime; long now = System.currentTimeMillis(); int delta = (int) (next - now); notifyQueue(delta); } } } // try { // mainLoop(0); // } finally { // // Someone killed this Thread, behave as if Timer cancelled // synchronized(queue) { // newTasksMayBeScheduled = false; // queue.clear(); // Eliminate obsolete references // } // } }
public void run() { try { mainLoop(); } finally { // Someone killed this Thread, behave as if Timer cancelled synchronized (queue) { newTasksMayBeScheduled= false; queue.clear(); // Eliminate obsolete references } } }
for (int i = queue.size(); i > 0; i--) { if (queue.get(i).state == TimerTask.CANCELLED) { queue.quickRemove(i); result++; queue.heapify();
synchronized(queue) { while (queue.isEmpty() && newTasksMayBeScheduled) break; if (queue.isEmpty()) break; // Queue is empty and will forever remain; die task = queue.getMin(); synchronized(task.lock) { if (task.state == TimerTask.CANCELLED) { queue.removeMin(); continue; // No action required, poll queue again if (taskFired = (executionTime<=currentTime)) { if (task.period == 0) { // Non-repeating, remove queue.removeMin(); task.state = TimerTask.EXECUTED; } else { // Repeating task, reschedule queue.rescheduleMin( task.period<0 ? currentTime - task.period : executionTime + task.period);
/** * Establishes the heap invariant (described above) in the entire tree, * assuming nothing about the order of the elements prior to the call. */ void heapify() { for (int i = size/2; i >= 1; i--) fixDown(i); } }
/** * Terminates this timer, discarding any currently scheduled tasks. * Does not interfere with a currently executing task (if it exists). * Once a timer has been terminated, its execution thread terminates * gracefully, and no more tasks may be scheduled on it. * * <p>Note that calling this method from within the run method of a * timer task that was invoked by this timer absolutely guarantees that * the ongoing task execution is the last task execution that will ever * be performed by this timer. * * <p>This method may be called repeatedly; the second and subsequent * calls have no effect. */ public void cancel() { synchronized(queue) { thread.newTasksMayBeScheduled = false; queue.clear(); thread.notifyQueue(1); // In case queue was already empty. } }
for (int i= queue.size(); i > 0; i--) if (queue.get(i).state == TimerTask.CANCELLED) queue.quickRemove(i); result++; queue.heapify();
/** * Remove the head task from the priority queue. */ void removeMin() { queue[1]= queue[size]; queue[size--]= null; // Drop extra reference to prevent memory leak fixDown(1); }
/** * Terminates this timer, discarding any currently scheduled tasks. * Does not interfere with a currently executing task (if it exists). * Once a timer has been terminated, its execution thread terminates * gracefully, and no more tasks may be scheduled on it. * * <p>Note that calling this method from within the run method of a * timer task that was invoked by this timer absolutely guarantees that * the ongoing task execution is the last task execution that will ever * be performed by this timer. * * <p>This method may be called repeatedly; the second and subsequent * calls have no effect. */ public void cancel() { synchronized(queue) { thread.newTasksMayBeScheduled = false; queue.clear(); thread.notifyQueue(1); // In case queue was already empty. } }
/** * Sets the nextExecutionTime associated with the head task to the * specified value, and adjusts priority queue accordingly. */ void rescheduleMin(long newTime) { queue[1].nextExecutionTime = newTime; fixDown(1); }
/** * Establishes the heap invariant (described above) in the entire tree, * assuming nothing about the order of the elements prior to the call. */ void heapify() { for (int i = size/2; i >= 1; i--) fixDown(i); } }