Your SlideShare is downloading. ×
Session13  J2ME Timer
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Session13 J2ME Timer

2,116
views

Published on

Session13 J2ME Timer

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,116
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Outline-session 13 (24-April-2009)
    • >> Scheduling Task
    • -Introduction
      • -Timer
      • -TimerTask
    • Sample:
      • -Timer Template
      • -Animation with Timer and TimerTask
  • 2. 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
  • 3. 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 date
    • public 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.
  • 4. 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
  • 5. 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.
  • 6. 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 milliseconds
    • 2. Fixed-delay that starts at a specific date
    • 3. Fixed-rate that starts in "n" number of milliseconds
    • 4. Fixed-rate that starts at a specific date
  • 7. Timer Tree Classification
  • 8. Timer Class API
  • 9. 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 option
      • 2. Create a class that extends TimerTask
      • 3. 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.....
    • }}
  • 10. Timer Task API
    • each of the scheduling methods has as the first parameter a reference to a TimerTask object
    • >>Sample TimerTemplate
  • 11. Animation with Timer/Timer Task
    • >> Animation.java
    • >> AnimationCanvas.java
    • >> AnimateTimerTask.java