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 |
|
|
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 |
475 |
} |
} |
476 |
|
|
477 |
/** |
/** |
478 |
|
* Create a new Timer whose Thread has the indicated name. It will have |
479 |
|
* normal priority and will not be a daemon thread. |
480 |
|
* @param name the name of the Thread |
481 |
|
* @since 1.5 |
482 |
|
*/ |
483 |
|
public Timer(String name) |
484 |
|
{ |
485 |
|
this(false, Thread.NORM_PRIORITY, name); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
489 |
|
* Create a new Timer whose Thread has the indicated name. It will have |
490 |
|
* normal priority. The boolean argument controls whether or not it |
491 |
|
* will be a daemon thread. |
492 |
|
* @param name the name of the Thread |
493 |
|
* @param daemon true if the Thread should be a daemon thread |
494 |
|
* @since 1.5 |
495 |
|
*/ |
496 |
|
public Timer(String name, boolean daemon) |
497 |
|
{ |
498 |
|
this(daemon, Thread.NORM_PRIORITY, name); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
502 |
* 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, |
503 |
* with the priority given and a default name. |
* with the priority given and a default name. |
504 |
*/ |
*/ |
691 |
{ |
{ |
692 |
queue.setNullOnEmpty(true); |
queue.setNullOnEmpty(true); |
693 |
} |
} |
694 |
|
|
695 |
|
/** |
696 |
|
* Removes all cancelled tasks from the queue. |
697 |
|
* @return the number of tasks removed |
698 |
|
* @since 1.5 |
699 |
|
*/ |
700 |
|
public int purge() |
701 |
|
{ |
702 |
|
return queue.purge(); |
703 |
|
} |
704 |
} |
} |