timer class in java

10

Click here to load reader

Upload: muthukumaran-subramanian

Post on 26-May-2015

55 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Timer class in java

Java.util package

Page 2: Timer class in java

Timer and TimerTask class

Page 3: Timer class in java

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.

Page 4: Timer class in java

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.

Page 5: Timer class in java

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.

Page 6: Timer class in java

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.

Page 7: Timer class in java

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.

Page 8: Timer class in java

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

Page 9: Timer class in java

Example program using Timer and TimerTask class

Page 10: Timer class in java

Sample program output