summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Tromey <tromey@redhat.com>2005-08-14 00:54:30 +0000
committerTom Tromey <tromey@redhat.com>2005-08-14 00:54:30 +0000
commit97e9f45ef9098c8f95b4ce7403c445f099e49c84 (patch)
treec275e0a402c303dccb4d52f6717c4cb9d54bf7ff
parent5d88c41547d7a6738118cf9fdd9c4445252274d5 (diff)
downloadclasspath-97e9f45ef9098c8f95b4ce7403c445f099e49c84.tar.gz
* java/util/Timer.java (Timer(String)): New constructor.
(Timer(String,boolean)): Likewise. (purge): New method. (TaskQueue.purge): Likewise.
-rw-r--r--ChangeLog7
-rw-r--r--java/util/Timer.java77
2 files changed, 82 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index a9f30317d..ad5df39c8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2005-08-13 Tom Tromey <tromey@redhat.com>
+ * java/util/Timer.java (Timer(String)): New constructor.
+ (Timer(String,boolean)): Likewise.
+ (purge): New method.
+ (TaskQueue.purge): Likewise.
+
+2005-08-13 Tom Tromey <tromey@redhat.com>
+
* java/util/FormattableFlags.java: New file.
2005-08-13 Tom Tromey <tromey@redhat.com>
diff --git a/java/util/Timer.java b/java/util/Timer.java
index 715f06cf6..b145d2b7c 100644
--- a/java/util/Timer.java
+++ b/java/util/Timer.java
@@ -1,5 +1,5 @@
/* Timer.java -- Timer that runs TimerTasks at a later time.
- Copyright (C) 2000, 2001 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2001, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -297,12 +297,67 @@ public class Timer
this.notify();
}
+ /**
+ * Remove all canceled tasks from the queue.
+ */
+ public synchronized int purge()
+ {
+ int removed = 0;
+ // Null out any elements that are canceled. Skip element 0 as
+ // it is the sentinel.
+ for (int i = elements; i > 0; --i)
+ {
+ if (heap[i].scheduled < 0)
+ {
+ ++removed;
+
+ // Remove an element by pushing the appropriate child
+ // into place, and then iterating to the bottom of the
+ // tree.
+ int index = i;
+ while (heap[index] != null)
+ {
+ int child = 2 * index;
+ if (child >= heap.length)
+ {
+ // Off end; we're done.
+ heap[index] = null;
+ break;
+ }
+
+ if (child + 1 >= heap.length || heap[child + 1] == null)
+ {
+ // Nothing -- we're done.
+ }
+ else if (heap[child] == null
+ || (heap[child].scheduled
+ > heap[child + 1].scheduled))
+ ++child;
+ heap[index] = heap[child];
+ index = child;
+ }
+ }
+ }
+
+ // Make a new heap if we shrank enough.
+ int newLen = heap.length;
+ while (elements - removed + DEFAULT_SIZE / 2 <= newLen / 4)
+ newLen /= 2;
+ if (newLen != heap.length)
+ {
+ TimerTask[] newHeap = new TimerTask[newLen];
+ System.arraycopy(heap, 0, newHeap, 0, elements + 1);
+ heap = newHeap;
+ }
+
+ return removed;
+ }
} // TaskQueue
/**
* The scheduler that executes all the tasks on a particular TaskQueue,
* reschedules any repeating tasks and that waits when no task has to be
- * executed immediatly. Stops running when canceled or when the parent
+ * executed immediately. Stops running when canceled or when the parent
* Timer has been finalized and no more tasks have to be executed.
*/
private static final class Scheduler implements Runnable
@@ -419,6 +474,18 @@ public class Timer
this(daemon, Thread.NORM_PRIORITY);
}
+ /** @since 1.5 */
+ public Timer(String name)
+ {
+ this(false, Thread.NORM_PRIORITY, name);
+ }
+
+ /** @since 1.5 */
+ public Timer(String name, boolean daemon)
+ {
+ this(daemon, Thread.NORM_PRIORITY, name);
+ }
+
/**
* Creates a new Timer with a daemon Thread as scheduler if daemon is true,
* with the priority given and a default name.
@@ -612,4 +679,10 @@ public class Timer
{
queue.setNullOnEmpty(true);
}
+
+ /** @since 1.5 */
+ public int purge()
+ {
+ return queue.purge();
+ }
}