java.util

Class Timer


public class Timer
extends Object

Timer that can run TimerTasks at a later time. TimerTasks can be scheduled for one time execution at some time in the future. They can be scheduled to be rescheduled at a time period after the task was last executed. Or they can be scheduled to be executed repeatedly at a fixed rate.

The normal scheduling will result in a more or less even delay in time between successive executions, but the executions could drift in time if the task (or other tasks) takes a long time to execute. Fixed delay scheduling guarantees more or less that the task will be executed at a specific time, but if there is ever a delay in execution then the period between successive executions will be shorter. The first method of repeated scheduling is preferred for repeated tasks in response to user interaction, the second method of repeated scheduling is preferred for tasks that act like alarms.

The Timer keeps a binary heap as a task priority queue which means that scheduling and serving of a task in a queue of n tasks costs O(log n).

Since:
1.3
See Also:
TimerTask

Constructor Summary

Timer()
Creates a new Timer with a non daemon Thread as Scheduler, with normal priority and a default name.
Timer(boolean daemon)
Creates a new Timer with a daemon Thread as scheduler if daemon is true, with normal priority and a default name.
Timer(String name)
Create a new Timer whose Thread has the indicated name.
Timer(String name, boolean daemon)
Create a new Timer whose Thread has the indicated name.

Method Summary

void
cancel()
Cancels the execution of the scheduler.
protected void
finalize()
Tells the scheduler that the Timer task died so there will be no more new tasks scheduled.
int
purge()
Removes all cancelled tasks from the queue.
void
schedule(TimerTask task, Date date)
Schedules the task at the specified data for one time execution.
void
schedule(TimerTask task, Date date, long period)
Schedules the task at the specified date and reschedules the task every period milliseconds after the last execution of the task finishes until this timer or the task is canceled.
void
schedule(TimerTask task, long delay)
Schedules the task after the specified delay milliseconds for one time execution.
void
schedule(TimerTask task, long delay, long period)
Schedules the task after the delay milliseconds and reschedules the task every period milliseconds after the last execution of the task finishes until this timer or the task is canceled.
void
scheduleAtFixedRate(TimerTask task, Date date, long period)
Schedules the task at the specified date and reschedules the task at a fixed rate every period milliseconds until this timer or the task is canceled.
void
scheduleAtFixedRate(TimerTask task, long delay, long period)
Schedules the task after the delay milliseconds and reschedules the task at a fixed rate every period milliseconds until this timer or the task is canceled.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Timer

public Timer()
Creates a new Timer with a non daemon Thread as Scheduler, with normal priority and a default name.

Timer

public Timer(boolean daemon)
Creates a new Timer with a daemon Thread as scheduler if daemon is true, with normal priority and a default name.

Timer

public Timer(String name)
Create a new Timer whose Thread has the indicated name. It will have normal priority and will not be a daemon thread.
Parameters:
name - the name of the Thread
Since:
1.5

Timer

public Timer(String name,
             boolean daemon)
Create a new Timer whose Thread has the indicated name. It will have normal priority. The boolean argument controls whether or not it will be a daemon thread.
Parameters:
name - the name of the Thread
daemon - true if the Thread should be a daemon thread
Since:
1.5

Method Details

cancel

public void cancel()
Cancels the execution of the scheduler. If a task is executing it will normally finish execution, but no other tasks will be executed and no more tasks can be scheduled.

finalize

protected void finalize()
            throws Throwable
Tells the scheduler that the Timer task died so there will be no more new tasks scheduled.
Overrides:
finalize in interface Object

purge

public int purge()
Removes all cancelled tasks from the queue.
Returns:
the number of tasks removed
Since:
1.5

schedule

public void schedule(TimerTask task,
                     Date date)
Schedules the task at the specified data for one time execution.
Throws:
IllegalArgumentException - if date.getTime() is negative
IllegalStateException - if the task was already scheduled or canceled or this Timer is canceled or the scheduler thread has died

schedule

public void schedule(TimerTask task,
                     Date date,
                     long period)
Schedules the task at the specified date and reschedules the task every period milliseconds after the last execution of the task finishes until this timer or the task is canceled.
Throws:
IllegalArgumentException - if period or date.getTime() is negative
IllegalStateException - if the task was already scheduled or canceled or this Timer is canceled or the scheduler thread has died

schedule

public void schedule(TimerTask task,
                     long delay)
Schedules the task after the specified delay milliseconds for one time execution.
Throws:
IllegalArgumentException - if delay or System.currentTimeMillis + delay is negative
IllegalStateException - if the task was already scheduled or canceled or this Timer is canceled or the scheduler thread has died

schedule

public void schedule(TimerTask task,
                     long delay,
                     long period)
Schedules the task after the delay milliseconds and reschedules the task every period milliseconds after the last execution of the task finishes until this timer or the task is canceled.
Throws:
IllegalArgumentException - if delay or period is negative
IllegalStateException - if the task was already scheduled or canceled or this Timer is canceled or the scheduler thread has died

scheduleAtFixedRate

public void scheduleAtFixedRate(TimerTask task,
                                Date date,
                                long period)
Schedules the task at the specified date and reschedules the task at a fixed rate every period milliseconds until this timer or the task is canceled.
Throws:
IllegalArgumentException - if period or date.getTime() is negative
IllegalStateException - if the task was already scheduled or canceled or this Timer is canceled or the scheduler thread has died

scheduleAtFixedRate

public void scheduleAtFixedRate(TimerTask task,
                                long delay,
                                long period)
Schedules the task after the delay milliseconds and reschedules the task at a fixed rate every period milliseconds until this timer or the task is canceled.
Throws:
IllegalArgumentException - if delay or System.currentTimeMillis + delay is negative
IllegalStateException - if the task was already scheduled or canceled or this Timer is canceled or the scheduler thread has died

Timer.java -- Timer that runs TimerTasks at a later time. Copyright (C) 2000, 2001, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.