session13 j2me timer

11
Outline-session 13 (24-April- 2009) >> Scheduling Task -Introduction -Timer -TimerTask Sample: -Timer Template -Animation with Timer and TimerTask

Upload: muthusvm

Post on 12-May-2015

2.139 views

Category:

Technology


1 download

DESCRIPTION

Session13 J2ME Timer

TRANSCRIPT

Page 1: Session13  J2ME Timer

Outline-session 13 (24-April-2009)

>> Scheduling Task -Introduction

-Timer-TimerTask

Sample:-Timer Template-Animation with Timer and TimerTask

Page 2: Session13  J2ME Timer

Scheduling task -Introduction>>Java version 1.3 added two classes— Timer and TimerTask —

to facilitate running of tasks in a background thread>>Timer are an abundance of scheduling options, ranging from

running a task once to repeating a task at regular intervals>>The classes Timer and TimerTask work hand-in-hand.>>Timer and Timer Task

--The timer specifies when a task is to be executed and the task is what is to be done--Timer class: Scheduling when a task will occur

--Timer Task class: Performing a task

Page 3: Session13  J2ME Timer

Scheduling Timer>> There are six scheduling methods available in the Timer class.>> Scheduling One-Time Tasks

1. Execute a task after a specific number of milliseconds

public void schedule (TimerTask task, long delay)2. Execute a task at a specific datepublic void schedule (TimerTask task, Date time)

>>Scheduling Repeating Tasks• Fixed-delay: Each execution of a task is based solely on how long it was

since the previous task finished.• Fixed-rate: Each execution of a task is based on when the first task started

and the requested delay between tasks.

Page 4: Session13  J2ME Timer

Scheduling Timer-Scenario >> There are six scheduling methods available in the Timer class.>> we create a timer that is to run a task every 60 seconds>> If the timer is delayed, the execution of the task will also be delayed>> if the Java garbage collector happens to run in between tasks, it may be

ore than 60 seconds before the next task is executed.Once the task is run, it will be another 60 seconds (at a minimum) before an additional task can occur. This ends up creating a cascading effect.

>> You can imagine that if such a timer were used to execute a task updating a clock, the accuracy of the clock would be suspect.

>> These timers are well-suited to tasks where consistency is more important than accuracy

Page 5: Session13  J2ME Timer

Scheduling Timer-Scenario >> one that is to execute a task every 60 seconds>> If more than one minute goes by before the system can execute the task,

there will be repeated calls to the task to "catch up" for any lost time>> These timers are best when accuracy is very important.>>For example, if a task needs to occur every hour on the hour, this type of

scheduling would be preferred.

Page 6: Session13  J2ME Timer

Scheduling Timer-Scenario >> one that is to execute a task every 60 seconds>> If more than one minute goes by before the system can execute the task,

there will be repeated calls to the task to "catch up" for any lost time>> These timers are best when accuracy is very important.>>For example, if a task needs to occur every hour on the hour, this type of

scheduling would be preferred.]Method Summary:1. Fixed-delay that starts in "n" number of milliseconds2. Fixed-delay that starts at a specific date3. Fixed-rate that starts in "n" number of milliseconds4. Fixed-rate that starts at a specific date

Page 7: Session13  J2ME Timer

Timer Tree Classification

Page 8: Session13  J2ME Timer

Timer Class API

Page 9: Session13  J2ME Timer

Timer Task• each of the scheduling methods has as the first parameter a reference to

a TimerTask object• The TimerTask class has just three methods, not including the constructor

– abstract void run()– public boolean cancel()– public long scheduledExecutionTime()

Putting the Pieces Together• There are three steps to make all this work.

1. Create a Timer with the appropriate scheduling option2. Create a class that extends TimerTask3. Write the run() method inside the above class

• // Allocate a timer Timer tm = new Timer();• // Allocate a task TodoTask tt = new TodoTask();• // Schedule the timer to execute in 1000 milliseconds tm.schedule(tt, 1000);• private class TodoTask extends TimerTask{public final void run(){do something.....• }}

Page 10: Session13  J2ME Timer

Timer Task API• each of the scheduling methods has as the first parameter a reference to

a TimerTask object

>>Sample TimerTemplate

Page 11: Session13  J2ME Timer

Animation with Timer/Timer Task>> Animation.java>> AnimationCanvas.java>> AnimateTimerTask.java