There is a new improvement in J2SE 1.3, which provides a timer class that can be more simple to implement multi-task scheduling, which is scheduled by a background thread. MIDP also contains this improvement so that J2ME developers benefit from it. J2ME prompts two classes to define and debug tasks, they are Timertask and Timer. TIMERTASK is an abstract base class defined by the user-defined task that needs to be scheduled. The Timer class is responsible for creating and managing execution threads when the task is executed. To define a task, define a subclass of Timertask and implement the RUN method. E.g
Import java.util. *;
Public Class MyTask Extends Timertask
{
Public void Run ()
{
System.out.Println ("Running The Task");
}
}
Is it very familiar with the RUN method? That is because Timertask implements a java.lang.Runnable interface. The Timer class calls this RUN method to perform each task. There is also something that must be noted, that is, the task performed by each RUN method must be able to terminate as soon as possible, because each Timer object can only perform one task at the same time. After defining a task, you can generate a Timer object and call the Schedule method to schedule it, just like the code presentation:
Import java.util. *;
Timer Timer = New Timer ();
Timertask Task = new mytask ();
// Wait for ten seconds before executing this task ...
Timer.schedule (Task, 10000);
// Wait for ten seconds before performing the task, then execute once every ten seconds
Timer.schedule (Task, 5000, 10000);
The Schedule method is overloaded four times; each task can be performed after a specific time point (specified using a DATE object) or the delay-specific time period (in milliseconds). You can arrange this task to perform only once or in a specific period of time repeatedly. The Timer also provides a scheduleatfixedrate method to specify a period of time periods that have been repeatedly executed according to the time executed in accordance with the task. If a task is delayed, the task arranged later is reducing the waiting time to "connect" delayed tasks accordingly. Each Timer object creates and manages a background thread. Under normal circumstances, a program creates a Timer enough, of course, can be created any more as needed. You can also stop a Timer at any time and terminate the background thread, and the method is to call the CANCEL method. But note that once the Timer is terminated, it is impossible to resume execution, unless you regenerate a Timer object and react the task you want to perform. The Timer object is a thread secure, you can access the Timer object directly in a multi-thread, without any explicit synchronization processing. In addition, each task provides a CANCEL method (inherited from the Timertask base class), you can call this method when the process is executed, and you can terminate the task. Once you have terminated this task, it will exit the task schedule. You can call each task at any time to terminate the execution of the task, even if the task has not been performed again. The following provides a briefed MIDlet example to demonstrate the use of Timer, we will use the timer to simulate a starry moving effect. The stars use a point to use this to use the low-border graphics API. For more detailed description of the low-critical graphics API, please refer to my other article "Use the MIDP's low-user interface API". Import javax.microedition.midlet. *;
Import javax.microedition.lcdui. *;
Import java.util. *;
Public class timerdemo extends midlet {
Display display;
Starfield Field = New Starfield ();
FieldMover Mover = New Fieldmover ();
Timer Timer = New Timer ();
Public TimerDemo () {
Display = display.getdisplay (this);
}
Protected void destroyApp (boolean unconditional) {
}
protected void startapp () {
Display.Setcurrent (Field);
Timer.schedule (Mover, 100, 100);
}
protected void pauseapp () {
}
Public void exit () {
Timer.cancel (); // stop scrolling
DESTROYAPP (TRUE);
NotifyDestroyed ();
}
Class Fieldmover Extends Timertask {
Public void run () {
Field.scroll ();
}
}
Class Starfield Extends Canvas {
INT height;
Int width;
Int [] stars;
Random generator = new random ();
Boolean painting = false;
Public starfield () {
Height = GetHeight ();
Width = getWidth ();
Stars = new int [height];
For (int i = 0; i STARS [I] = -1; } } Public void scroll () { IF (Painting) return; For (int i = height-1; i> 0; --I) { Stars [i] = stars [i-1]; } Stars [0] = (generator.nextint ()% (3 * width)) / 2; IF (stars [0]> = width) { STARS [0] = -1; } Repaint (); } Protected Void Paint (Graphics G) { Painting = true; G.SetColor (0, 0, 0); g.fillRect (0, 0, Width, Height); G.SetColor (255, 255, 255); FOR (int y = 0; y INT x = stars [y]; IF (x == -1) Continue; g.drawline (x, y, x, y); } Painting = false; } protected void keypressed (int keycode) { exit (); } } } TimerDemo MIDLET uses a Timer object timer to schedule an Timertask task FieldMover, the time gap is 100 milliseconds. Fieldmover processes the update of the starry and redes the task, so that the entire starry is constantly "extension" under the screen. This generates a simple star moving effect.