session13 j2me timer

Post on 12-May-2015

2.139 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Session13 J2ME Timer

TRANSCRIPT

Outline-session 13 (24-April-2009)

>> Scheduling Task -Introduction

-Timer-TimerTask

Sample:-Timer Template-Animation with Timer and TimerTask

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

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.

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

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.

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

Timer Tree Classification

Timer Class API

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.....• }}

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

a TimerTask object

>>Sample TimerTemplate

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

top related