Timers
If you want to execute tasks that take different amounts of time during sprite movement and collision detection, the simplest way is to use the java.util.Timer class. The Timer class makes it easy for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which might bunch up and execute in rapid succession when the offending task finally completes. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the timer's cancel() method.
The following methods are part of the Timer class:
- schedule(TimerTask task, long delay) — This method schedules the specified task for execution after the specified delay. Games should call this method when execution of the task doesn't depend on fixed execution rate. Each task is executed within a specified amount of time (for example, every 100 milliseconds), but the execution of the task can exceed that time.
- schedule(TimerTask task, Date time) — This method schedules the specified task for execution at the specified time.
- schedule(TimerTask task, long delay, long period) — This method schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
If the execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period.
- schedule(TimerTask task, Date firstTime, long period) — This method schedules the specified task for repeated fixed-delay execution, beginning at the specified time. This is similar to preceding method.
- scheduleAtFixedRate(TimerTask task, long delay, long period)— This method schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period.
If the execution is delayed for any reason, two or more executions will occur in rapid succession to catch up. In the long run, the frequency of execution will be the exact reciprocal of the specified period.
A game should use this method if the timeframe must be fixed. However, if the execution time of the task is larger than is allowed, the number of threads will constantly increase. J2ME devices are unfortunately resource-limited, and might eventually crash.
- scheduleAtFixedRate(TimerTask task, Date firstTime, long period) — This method schedules the specified task for repeated fixed-rate execution, beginning at the specified time. This is similar to the preceding method.
- cancel() — This method terminates this timer, discarding any currently scheduled tasks. It does not interfere with a currently executing task if it exists. After a timer has been terminated, its execution thread terminates gracefully, and no more tasks can be scheduled on it.
You need to implement your own TimerTask class and create a TimerTask object if you want to use the Timer class. The run() method needs to be implemented containing the thread functionality. Listing 12.3 contains an implementation of threading using the Timer class.
Listing 12.3 The GameCanvas Timer Example import java.util.*;
public class GameCanvas {
private final int DELAY = 100; private Timer timer;
public GameCanvas() {
public void start() {
GameTask task = new GameTask(); timer = new Timer();
timer.scheduleAtFixedRate(task, 0, DELAY);
public void stop() {
running = false;
public class GameTask extends TimerTask {
// Move sprites // Check collisions // Repaint
This code is similar to the implementation of the Thread class, where the developer writes Thread's own inner class. Timer executes the task every 100 milliseconds. If the task takes more then 100 milliseconds, a new task will still be invoked on time. This means that at a specific time, there might be a bunch of threads running, which can slow down the device. Another approach would be to use the schedule() method, but the application would lose the fixed scheduling necessary during game execution.
Post a comment