classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] [generics] Patch: FYI: update Timer


From: Tom Tromey
Subject: [cp-patches] [generics] Patch: FYI: update Timer
Date: 13 Aug 2005 18:49:55 -0600

I'm checking this in on the generics branch.

This adds the new 1.5 methods to java.util.Timer.

Tom

Index: ChangeLog
from  Tom Tromey  <address@hidden>
        * java/util/Timer.java (Timer(String)): New constructor.
        (Timer(String,boolean)): Likewise.
        (purge): New method.
        (TaskQueue.purge): Likewise.

Index: java/util/Timer.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Timer.java,v
retrieving revision 1.11.2.4
diff -u -r1.11.2.4 Timer.java
--- java/util/Timer.java 2 Aug 2005 20:12:30 -0000 1.11.2.4
+++ java/util/Timer.java 14 Aug 2005 00:52:55 -0000
@@ -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 @@
       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 @@
     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.
@@ -611,5 +678,11 @@
   protected void finalize() throws Throwable
   {
     queue.setNullOnEmpty(true);
+  }
+
+  /** @since 1.5 */
+  public int purge()
+  {
+    return queue.purge();
   }
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]