/[classpath]/classpath/java/util/Timer.java
ViewVC logotype

Diff of /classpath/java/util/Timer.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.11.2.4 by gnu_andrew, Tue Aug 2 20:12:30 2005 UTC revision 1.11.2.5 by tromey, Sun Aug 14 00:54:32 2005 UTC
# Line 1  Line 1 
1  /* Timer.java -- Timer that runs TimerTasks at a later time.  /* Timer.java -- Timer that runs TimerTasks at a later time.
2     Copyright (C) 2000, 2001 Free Software Foundation, Inc.     Copyright (C) 2000, 2001, 2005 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 297  public class Timer Line 297  public class Timer
297        this.notify();        this.notify();
298      }      }
299    
300        /**
301         * Remove all canceled tasks from the queue.
302         */
303        public synchronized int purge()
304        {
305          int removed = 0;
306          // Null out any elements that are canceled.  Skip element 0 as
307          // it is the sentinel.
308          for (int i = elements; i > 0; --i)
309            {
310              if (heap[i].scheduled < 0)
311                {
312                  ++removed;
313    
314                  // Remove an element by pushing the appropriate child
315                  // into place, and then iterating to the bottom of the
316                  // tree.
317                  int index = i;
318                  while (heap[index] != null)
319                    {
320                      int child = 2 * index;
321                      if (child >= heap.length)
322                        {
323                          // Off end; we're done.
324                          heap[index] = null;
325                          break;
326                        }
327    
328                      if (child + 1 >= heap.length || heap[child + 1] == null)
329                        {
330                          // Nothing -- we're done.
331                        }
332                      else if (heap[child] == null
333                               || (heap[child].scheduled
334                                   > heap[child + 1].scheduled))
335                        ++child;
336                      heap[index] = heap[child];
337                      index = child;
338                    }
339                }
340            }
341    
342          // Make a new heap if we shrank enough.
343          int newLen = heap.length;
344          while (elements - removed + DEFAULT_SIZE / 2 <= newLen / 4)
345            newLen /= 2;
346          if (newLen != heap.length)
347            {
348              TimerTask[] newHeap = new TimerTask[newLen];
349              System.arraycopy(heap, 0, newHeap, 0, elements + 1);
350              heap = newHeap;
351            }
352    
353          return removed;
354        }
355    }                             // TaskQueue    }                             // TaskQueue
356    
357    /**    /**
358     * The scheduler that executes all the tasks on a particular TaskQueue,     * The scheduler that executes all the tasks on a particular TaskQueue,
359     * reschedules any repeating tasks and that waits when no task has to be     * reschedules any repeating tasks and that waits when no task has to be
360     * executed immediatly. Stops running when canceled or when the parent     * executed immediately. Stops running when canceled or when the parent
361     * Timer has been finalized and no more tasks have to be executed.     * Timer has been finalized and no more tasks have to be executed.
362     */     */
363    private static final class Scheduler implements Runnable    private static final class Scheduler implements Runnable
# Line 419  public class Timer Line 474  public class Timer
474      this(daemon, Thread.NORM_PRIORITY);      this(daemon, Thread.NORM_PRIORITY);
475    }    }
476    
477      /** @since 1.5 */
478      public Timer(String name)
479      {
480        this(false, Thread.NORM_PRIORITY, name);
481      }
482    
483      /** @since 1.5 */
484      public Timer(String name, boolean daemon)
485      {
486        this(daemon, Thread.NORM_PRIORITY, name);
487      }
488    
489    /**    /**
490     * Creates a new Timer with a daemon Thread as scheduler if daemon is true,     * Creates a new Timer with a daemon Thread as scheduler if daemon is true,
491     * with the priority given and a default name.     * with the priority given and a default name.
# Line 612  public class Timer Line 679  public class Timer
679    {    {
680      queue.setNullOnEmpty(true);      queue.setNullOnEmpty(true);
681    }    }
682    
683      /** @since 1.5 */
684      public int purge()
685      {
686        return queue.purge();
687      }
688  }  }

Legend:
Removed from v.1.11.2.4  
changed lines
  Added in v.1.11.2.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26