TimerThread newThread = new TimerThread(); newThread.start();
protected void finalize() throws Throwable { synchronized(queue) { thread.newTasksMayBeScheduled = false; thread.notifyQueue(1); // In case queue is empty. } } };
/** * Creates a new timer whose associated thread has the specified name. * The associated thread does <i>not</i> * {@linkplain Thread#setDaemon run as a daemon}. * * @param name the name of the associated thread * @throws NullPointerException if {@code name} is null * @since 1.5 */ public Timer(String name) { thread.setName(name); thread.start(); }
@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 } } }
notifyQueue((int)(executionTime - currentTime)); return; log("Running " + task); task.run(); log("Done running " + task);
void notifyQueue(int delay) { if (delay < 1) { delay = 1; } prevTimeout = setTimeout(delay, this, prevTimeout); }
@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 // } // } }
notifyQueue((int)(executionTime - currentTime)); return; log("Running " + task); task.run(); log("Done running " + task);
void notifyQueue(int delay) { if (delay < 1) { delay = 1; } prevTimeout = setTimeout(delay, this, prevTimeout); }
/** * Creates a new timer whose associated thread has the specified name, * and may be specified to * {@linkplain Thread#setDaemon run as a daemon}. * * @param name the name of the associated thread * @param isDaemon true if the associated thread should run as a daemon * @throws NullPointerException if {@code name} is null * @since 1.5 */ public Timer(String name, boolean isDaemon) { thread.setName(name); thread.start(); }
this.myRefreshThread = new Thread(new TimerThread()); this.myRefreshThread.start();
protected void finalize() throws Throwable { synchronized(queue) { thread.newTasksMayBeScheduled = false; thread.notifyQueue(1); // In case queue is empty. } } };
/** * 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. } }
/** * 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. } }
thread.notifyQueue(1);