Class Timer
- java.lang.Object
-
- java.util.Timer
-
public class Timer extends Object
Class to schedule tasks in a separate thread.Each timer is associated with precisely one “execution thread”, which it uses to run
TimerTaskinstances serially. ScheduledTimerTaskinstances may either be one-shot or recurring. Recurring tasks can be scheduled as fixed-period withschedule(java.util.TimerTask, java.util.Date)or as fixed-rate withscheduleAtFixedRate(java.util.TimerTask, long, long).Timers are thread safe in the sense that several threads can operate on one instance at the same time.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel()Cancels all remaining tasks on the timer.protected voidfinalize()Make sure the thread is stopped when the timer is garbage-collected.intpurge()Removes all references held by this instance to cancelled timer-tasks.voidschedule(TimerTask task, long delay)Schedules a one-shot task after a fixed delaydelay, specified in milliseconds.voidschedule(TimerTask task, long delay, long period)Schedules a recurring fixed-period task after a fixed delaydelaywith periodperiod.voidschedule(TimerTask task, Date when)Schedules a one-shot task at a fixed timewhen.voidschedule(TimerTask task, Date when, long period)Schedules a recurring fixed-period task after a fixed timewhenwith periodperiod.voidscheduleAtFixedRate(TimerTask task, long delay, long period)Schedules a recurring fixed-rate task after a fixed delaydelaywith periodperiod.voidscheduleAtFixedRate(TimerTask task, Date when, long period)Schedules a recurring fixed-rate task after a fixed timewhenwith periodperiod.
-
-
-
Constructor Detail
-
Timer
public Timer(String name, boolean isDaemon)
Create a new timer with namename.isDaemonspecifies whether the execution thread is a daemon or not.
-
Timer
public Timer(String name)
Equivalent tothis(name, false).
-
Timer
public Timer(boolean isDaemon)
Creates a new timer with a default (unique) name.
-
Timer
public Timer()
Equivalent tothis(false)
-
-
Method Detail
-
finalize
protected void finalize() throws ThrowableMake sure the thread is stopped when the timer is garbage-collected.
-
cancel
public void cancel()
Cancels all remaining tasks on the timer.The task that’s currently running (if any) must be allowed to complete. No further tasks can be enqueued on this timer. Also, releases all resources associated with this timer. All subsequent calls to methods on this instance must be no-ops.
-
purge
public int purge()
Removes all references held by this instance to cancelled timer-tasks.Those tasks will be collectible at the end of this method if no other references to them exist. Returns the number of tasks purged.
-
schedule
public void schedule(TimerTask task, Date when)
Schedules a one-shot task at a fixed timewhen.
-
schedule
public void schedule(TimerTask task, long delay)
Schedules a one-shot task after a fixed delaydelay, specified in milliseconds.
-
schedule
public void schedule(TimerTask task, long delay, long period)
Schedules a recurring fixed-period task after a fixed delaydelaywith periodperiod.Both delay and period are specified in milliseconds.
-
schedule
public void schedule(TimerTask task, Date when, long period)
Schedules a recurring fixed-period task after a fixed timewhenwith periodperiod.Period is specified in milliseconds.
-
scheduleAtFixedRate
public void scheduleAtFixedRate(TimerTask task, long delay, long period)
Schedules a recurring fixed-rate task after a fixed delaydelaywith periodperiod.Both delay and period are specified in milliseconds.
-
-