Java Reference
In-Depth Information
int x1 = x0;
int h = (mrh [y] * scy) >> 8;
for (int x = 0; x < cols && i < count; x++) {
int w = (mcw [x] * scx) >> 8;
container.getComponent (i++).setBounds (x1, y0, w, h);
x1 += w + 1;
}
y0 += h + 1;
}
}
}
Multiple Threads in the PDAP AWT Subset
As mentioned in the beginning of this chapter, one of the main restrictions of the PDAP AWT subset is
that AWT methods might be called from the event dispatching thread only. This restriction is necessary
because the implementation of a thread-safe AWT subset is more complex and slower than an AWT
subset based on the single thread model. Actually, the modern J2ME SWING user interface doesn't
allow calls from multiple threads for the same reasons. However, this does not mean that the PDAP
AWT cannot work with threads at all. It is just necessary to take some extra steps when making calls to
AWT methods from separate threads.
For calls from separate threads, the AWT class
EventQueue
provides two static methods,
invokeLater()
and
invokeAndWait()
. Both methods take an object implementing the
Runnable
interface as parameter. Calls to these methods can be performed from threads other than
the event handling thread. AWT automatically ensures that the
run()
method of the given
Runnable
class is then called from the AWT event thread. The difference between
invokeLater()
and
invokeAndWait()
is that
invokeLater()
returns immediately, whereas
invokeAndWait()
does not return until the
run()
method of the given object has been executed.
An example using multiple threads is a simulation running in a separate thread, which needs to update
some components showing the simulation state from time to time.
The
PdaLander
application is a simplified simulation of a lunar landing. It displays the landing
parameters such as altitude, velocity, and fuel remaining and allows the user to set the thrust of the
engine to a value between 0 and 100%.
The core of the application, the simulation thread can be implemented as an inner class of the
PdaLander
application, accessing the simulation state variables
velocity
,
height
,
fuel
, and
thrust
. The constants
GRAVITY
and
ACCELERATION
reflect the gravity of the moon and the
acceleration available at 100% thrust. Note that all values are measured in fine-grained units in order to
avoid slow floating-point operations. All values are measured in metric units in order to simplify the
calculations and to save our lunar lander from the fate of the Mars Polar lander.
The simulation is performed by measuring the elapsed time since the last simulation step and then
updating all variables accordingly. The current velocity is recalculated based on the acceleration and
time, the height is calculated based on the velocity and time, and finally the remaining fuel is adjusted.
Then the
invokeAndWait
() method is called in order to update the user interface, ensuring
synchronization with the AWT event thread:.
import java.awt.*;
import java.awt.event.*;
import javax.microedition.midlet.*;
public class PdaLander extends MIDlet {
static final long GRAVITY = 1620; // mm/s2
static final long ACCELERATION = 2*GRAVITY;
Search WWH ::
Custom Search