summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2005-11-14 20:47:07 +0000
committerMark Wielaard <mark@klomp.org>2005-11-14 20:47:07 +0000
commit0f055ba8cf874b84bae78c1eb478c521248f8c6a (patch)
tree9c7dba547390f48a41089c9a536ba6025e2f64ae
parenteeb751391d053f07ceb0be04c9527723d710fe59 (diff)
downloadclasspath-0f055ba8cf874b84bae78c1eb478c521248f8c6a.tar.gz
As suggested by Joao Victor <jvital@gmail.com>:
* javax/swing/Timer.java (Waker): Removed class. (Task): New class. (timer): New field. (running): Removed field. (waker): Likewise. (task): New field. (isRunning): Check whether task is null. (start): Create task and schedule it with timer. (stop): Cancel task and clear field. (queueEvent): Synchronized on queueLock.
-rw-r--r--ChangeLog14
-rw-r--r--javax/swing/Timer.java125
2 files changed, 58 insertions, 81 deletions
diff --git a/ChangeLog b/ChangeLog
index 3bb9c025e..494dd9ba0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2005-11-14 Mark Wielaard <mark@klomp.org>
+
+ As suggested by Joao Victor <jvital@gmail.com>:
+ * javax/swing/Timer.java (Waker): Removed class.
+ (Task): New class.
+ (timer): New field.
+ (running): Removed field.
+ (waker): Likewise.
+ (task): New field.
+ (isRunning): Check whether task is null.
+ (start): Create task and schedule it with timer.
+ (stop): Cancel task and clear field.
+ (queueEvent): Synchronized on queueLock.
+
2005-11-14 Lillian Angel <langel@redhat.com>
* javax/swing/JTree.java
diff --git a/javax/swing/Timer.java b/javax/swing/Timer.java
index 8a4cd0a79..cf91c23e8 100644
--- a/javax/swing/Timer.java
+++ b/javax/swing/Timer.java
@@ -46,7 +46,12 @@ import java.util.EventListener;
import javax.swing.event.EventListenerList;
/**
- * Fires one or more action events after the specified delay.
+ * Fires one or more action events after the specified delay. This is
+ * a specialised version of <code>java.util.Timer</code> just for
+ * firing <code>ActionEvent</code>s. All Timers share one (daemon)
+ * Thread (or java.util.Timer). All events are fired from the event
+ * queue.
+ *
* @author Ronald Veldema
* @author Audrius Meskauskas (audriusa@Bionformatics.org) - bug fixes
* and documentation comments
@@ -55,64 +60,19 @@ public class Timer
implements Serializable
{
/**
- * The timer thread
+ * Given to the shared java.util.Timer to (possibly repeatedly) call
+ * queueEvent().
*/
- private class Waker
- extends Thread
+ private class Task extends java.util.TimerTask
{
- /**
- * Fires events, pausing for required intervals.
- */
public void run()
{
- synchronized (queueLock)
- {
- try
- {
- try
- {
- queueLock.wait(initialDelay);
- }
- catch (InterruptedException e)
- {
- // Ignored
- }
-
- if (!running)
- return;
-
- queueEvent();
-
- if (repeats)
- while (running)
- {
- try
- {
- queueLock.wait(delay);
- }
- catch (InterruptedException e)
- {
- // Ignored
- }
-
- if (!running)
- break;
-
- queueEvent();
-
- if (logTimers)
- System.out.println("javax.swing.Timer -> clocktick");
-
- if (!repeats)
- break;
- }
- }
- finally
- {
- // The timer is no longer running.
- running = false;
- }
- }
+ if (logTimers)
+ System.out.println("javax.swing.Timer -> queueEvent()");
+ queueEvent();
+
+ if (!repeats)
+ task = null;
}
}
@@ -134,6 +94,14 @@ public class Timer
};
/**
+ * The static java.util.Timer daemon which will be used to schedule
+ * all javax.swing.Timer.Task objects. The daemon will always be
+ * running, even if there's no task scheduled in it.
+ */
+ private static java.util.Timer timer = new java.util.Timer("swing.Timer",
+ true);
+
+ /**
* If <code>true</code>, the timer prints a message to
* {@link System#out} when firing each event.
*/
@@ -155,12 +123,6 @@ public class Timer
boolean repeats = true;
/**
- * <code>true</code> if the timer is currently active, firing events
- * as scheduled. Should only be checked/set with queueLock held.
- */
- boolean running;
-
- /**
* The delay between subsequent repetetive events.
*/
int delay;
@@ -178,10 +140,9 @@ public class Timer
int ticks;
/**
- * Stores the thread that posts events to the queue at required time
- * intervals.
+ * The task that calls queueEvent(). When null this Timer is stopped.
*/
- private Waker waker;
+ private Task task;
/**
* This object manages a "queue" of virtual actionEvents, maintained as a
@@ -376,7 +337,7 @@ public class Timer
*/
public boolean isRunning()
{
- return running;
+ return task != null;
}
/**
@@ -414,14 +375,15 @@ public class Timer
*/
public void start()
{
- synchronized (queueLock)
+ Task t = task;
+ if (t == null)
{
- if (!running)
- {
- running = true;
- waker = new Waker();
- waker.start();
- }
+ t = new Task();
+ if (isRepeats())
+ timer.schedule(t, getInitialDelay(), getDelay());
+ else
+ timer.schedule(t, getInitialDelay());
+ task = t;
}
}
@@ -430,12 +392,11 @@ public class Timer
*/
public void stop()
{
- synchronized (queueLock)
+ Task t = task;
+ if (t != null)
{
- running = false;
- queue = 0;
- queueLock.notifyAll();
- waker = null;
+ t.cancel();
+ task = null;
}
}
@@ -493,12 +454,14 @@ public class Timer
/**
* Post a scheduled event to the event queue.
* Package-private to avoid an accessor method.
- * Called with queueLock held and running = true from Waker.run().
*/
void queueEvent()
{
- queue++;
- if (queue == 1)
- SwingUtilities.invokeLater(drainer);
+ synchronized(queueLock)
+ {
+ queue++;
+ if (queue == 1)
+ SwingUtilities.invokeLater(drainer);
+ }
}
}