Java Reference
In-Depth Information
public void wakeUp() {
try {
synchronized (lock)
{
lock.notify();
}
} catch (Exception e) {
form.append("err: " + e);
}
} private void waitForNotify() {
new Thread() {
public void run()
{
try {
form.append(" \ nzzz...");
synchronized (lock)
{
lock.wait();
} form.append(" \ noy!");
} catch (Exception e) {
form.append("err: " + e);
}
}
} .start();
}
MIDlet2 invokes waitForNotify() when we press a new button:
if (cmd == waitCmd) {
this.waitForNotify();
}
And now comes the fun part - we let MIDlet1 invoke MIDlet2.
wakeUp() :
if (cmd == notifyCmd) {
MIDlet2.instance.wakeUp();
}
Let's run the example. We start both MIDlet1 and MIDlet2. In MIDlet2,
we press Wait and now there is a thread in MIDlet2 which is waiting.
We switch back to MIDlet1 and press Wake up. When we move back
to MIDlet2, we see the screen in Figure 3.8. During all that time, there
can be other applications, Java or native, running in the background (see
Figure 3.9).
Multiple applications can run concurrently and multiple applications
from the same suite even run in the same space. There is power here that
you can use but which should be handled with care.
Search WWH ::




Custom Search