Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Java.util package
Timer and TimerTask class
Timer class
● The java.util.Timer class provides facility for threads to
schedule tasks for future execution in a backgrou...
Constructors in Timer class
Constructor Description
Timer() This constructor creats a new timer
Timer(boolean isDaemon) Th...
Methods in Timer class
Methods Description
Void cancel() This method terminates this
timer,discarding any currently schedu...
Cont'd
Methods Description
Void scheduleAtFixedRate(TimerTask
task,Date firstTime,long period)
This method schedules the s...
TimerTask class
● The java.util.TimerTask class represents a task
that can be scheduled for one-time or repeated
execution...
Methods in TimerTask class
Methods Description
Boolean cancel() This method cancels this timer task
Abstract void run() Th...
Example program using Timer and
TimerTask class
Sample program output
Upcoming SlideShare
Loading in …5
×

Timer class in java

519 views

Published on

  • Be the first to comment

Timer class in java

  1. 1. Java.util package
  2. 2. Timer and TimerTask class
  3. 3. Timer class ● The java.util.Timer class provides facility for threads to schedule tasks for future execution in a background thread. ● This class is thread-safe i.e multiple threads can share a single Timer object without the need for external synchronization. ● This class schedules tasks for one-time execution, or for repeated execution at regular intervals. ● All constructors start a timer thread.
  4. 4. Constructors in Timer class Constructor Description Timer() This constructor creats a new timer Timer(boolean isDaemon) This constructor creates a new timer whose associated thread may be specified to run as a daemon Timer(String name) This constructor creates a new timer whose associated thread has the specified name Timer(String name, boolean isDaemon) This constructor creates a nw timer whose associated thread has the specified name , and may be specified to run as a daemon.
  5. 5. Methods in Timer class Methods Description Void cancel() This method terminates this timer,discarding any currently scheduled tasks. Int purge() This method removes all cancelled tasks from this timer's task queue Void schedule(TimerTask task, Date time) This method schedules the specified task for execution at the specified time Void schedule(TiemrTask task, Date firstTime, long period) This method schedules the specified task for repeated fixed-delay execution, beginning at the specified time Void schedule(TimerTask task, long delay) This method schedules the specified task for exectuiton after the specified delay Void schedule(TimerTask task,long delay, long period) This method schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
  6. 6. Cont'd Methods Description Void scheduleAtFixedRate(TimerTask task,Date firstTime,long period) This method schedules the specified task for repeated fixed-rate exectuion, begining at the specified time Void scheduleAtFixedRate(TimerTask task, long delay, long period) This method schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
  7. 7. TimerTask class ● The java.util.TimerTask class represents a task that can be scheduled for one-time or repeated execution by a Timer. protected TimerTask()-This constructor creates a new timer task.
  8. 8. Methods in TimerTask class Methods Description Boolean cancel() This method cancels this timer task Abstract void run() This method represents the action to be performed by this timer task. Long scheduledExecutionTime() This method returns the scheduled execution time of the most recent actual execution task
  9. 9. Example program using Timer and TimerTask class
  10. 10. Sample program output

×