Session13 J2ME Timer

2,428 views

Published on

Session13 J2ME Timer

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,428
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Session13 J2ME Timer

  1. 1. Outline-session 13 (24-April-2009) <ul><li>>> Scheduling Task </li></ul><ul><li> -Introduction </li></ul><ul><ul><li>-Timer </li></ul></ul><ul><ul><li>-TimerTask </li></ul></ul><ul><li>Sample: </li></ul><ul><ul><li>-Timer Template </li></ul></ul><ul><ul><li>-Animation with Timer and TimerTask </li></ul></ul>
  2. 2. Scheduling task -Introduction <ul><li>>>Java version 1.3 added two classes— Timer and TimerTask —to facilitate running of tasks in a background thread </li></ul><ul><li>>>Timer are an abundance of scheduling options, ranging from running a task once to repeating a task at regular intervals </li></ul><ul><li>>>The classes Timer and TimerTask work hand-in-hand. </li></ul><ul><li>>>Timer and Timer Task </li></ul><ul><li>--The timer specifies when a task is to be executed and the task is what is to be done </li></ul><ul><li>--Timer class: Scheduling when a task will occur </li></ul><ul><li>-- Timer Task class: Performing a task </li></ul>
  3. 3. Scheduling Timer <ul><li>>> There are six scheduling methods available in the Timer class. </li></ul><ul><li>>> Scheduling One-Time Tasks </li></ul><ul><li>1. Execute a task after a specific number of milliseconds </li></ul><ul><li>public void schedule (TimerTask task, long delay) </li></ul><ul><li>2. Execute a task at a specific date </li></ul><ul><li>public void schedule (TimerTask task, Date time) </li></ul><ul><li>>>Scheduling Repeating Tasks </li></ul><ul><li>Fixed-delay: Each execution of a task is based solely on how long it was since the previous task finished. </li></ul><ul><li>Fixed-rate: Each execution of a task is based on when the first task started and the requested delay between tasks. </li></ul>
  4. 4. Scheduling Timer-Scenario <ul><li>>> There are six scheduling methods available in the Timer class. </li></ul><ul><li>>> we create a timer that is to run a task every 60 seconds </li></ul><ul><li>>> If the timer is delayed, the execution of the task will also be delayed </li></ul><ul><li>>> 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. </li></ul><ul><li>>> 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. </li></ul><ul><li>>> These timers are well-suited to tasks where consistency is more important than accuracy </li></ul>
  5. 5. Scheduling Timer-Scenario <ul><li>>> one that is to execute a task every 60 seconds </li></ul><ul><li>>> If more than one minute goes by before the system can execute the task, there will be repeated calls to the task to &quot;catch up&quot; for any lost time </li></ul><ul><li>>> These timers are best when accuracy is very important. </li></ul><ul><li>>>For example, if a task needs to occur every hour on the hour, this type of scheduling would be preferred. </li></ul>
  6. 6. Scheduling Timer-Scenario <ul><li>>> one that is to execute a task every 60 seconds </li></ul><ul><li>>> If more than one minute goes by before the system can execute the task, there will be repeated calls to the task to &quot;catch up&quot; for any lost time </li></ul><ul><li>>> These timers are best when accuracy is very important. </li></ul><ul><li>>>For example, if a task needs to occur every hour on the hour, this type of scheduling would be preferred.] </li></ul><ul><li>Method Summary: </li></ul><ul><li>1. Fixed-delay that starts in &quot;n&quot; number of milliseconds </li></ul><ul><li>2. Fixed-delay that starts at a specific date </li></ul><ul><li>3. Fixed-rate that starts in &quot;n&quot; number of milliseconds </li></ul><ul><li>4. Fixed-rate that starts at a specific date </li></ul>
  7. 7. Timer Tree Classification
  8. 8. Timer Class API
  9. 9. Timer Task <ul><li>each of the scheduling methods has as the first parameter a reference to a TimerTask object </li></ul><ul><li>The TimerTask class has just three methods, not including the constructor </li></ul><ul><ul><li>abstract void run() </li></ul></ul><ul><ul><li>public boolean cancel() </li></ul></ul><ul><ul><li>public long scheduledExecutionTime() </li></ul></ul><ul><li>Putting the Pieces Together </li></ul><ul><li>There are three steps to make all this work. </li></ul><ul><ul><li>1. Create a Timer with the appropriate scheduling option </li></ul></ul><ul><ul><li>2. Create a class that extends TimerTask </li></ul></ul><ul><ul><li>3. Write the run() method inside the above class </li></ul></ul><ul><li>// Allocate a timer Timer tm = new Timer(); </li></ul><ul><li>// Allocate a task TodoTask tt = new TodoTask(); </li></ul><ul><li>// Schedule the timer to execute in 1000 milliseconds tm.schedule(tt, 1000); </li></ul><ul><li>private class TodoTask extends TimerTask{public final void run(){do something..... </li></ul><ul><li>}} </li></ul>
  10. 10. Timer Task API <ul><li>each of the scheduling methods has as the first parameter a reference to a TimerTask object </li></ul><ul><li>>>Sample TimerTemplate </li></ul>
  11. 11. Animation with Timer/Timer Task <ul><li>>> Animation.java </li></ul><ul><li>>> AnimationCanvas.java </li></ul><ul><li>>> AnimateTimerTask.java </li></ul>

×