j2me thread wait/notify question

S

Stee1HeD

With the following test I am trying to synchronize and wait on the
String field 'status' in LCDThreads. In the method 'startApp' when
'synchronized' or 'wait' is used, there is no display output, but the
thread in class1 is running becauase every 5 seconds I get the
System.out.println message.

I am trying to find a way to 'pause' a current display, call another
display with a textbox, and return to the initial display with the
contents of the textbox, like a dialog. Displays seem to be awefully
touchy and only work when they are allowed to 'complete' the method
without any Thread.sleep or wait or anything. Am I to refactor all my
code into different methods when getting input from my dialog screen?
Is there a better way to do this?

Please help!

=============================================
=============================================

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class LCDThreads extends MIDlet implements CommandListener{
Command ok1;
Command ok2;

Class1 class1;
Class2 class2;

static StringItem tb;

Form form;
Display myDisplay;

static String status; //I synchronize on this field

public LCDThreads() {
super();
}

protected void startApp() throws MIDletStateChangeException {
myDisplay = Display.getDisplay(this);

form = new Form("the form");

tb = new StringItem("String item","");

ok2 = new Command("OK2",Command.OK,1);
ok1 = new Command("OK1",Command.OK,1);

form.append(tb);
form.addCommand(ok2);
form.addCommand(ok1);

tb.setText("0 : count = " + Thread.activeCount() + " : " +
Thread.currentThread());

myDisplay.setCurrent(form);

form.setCommandListener(this);

class1 = new Class1(this,form);
class1.start();

System.out.println("at the beginning of the party");
try {
synchronized(status){
System.out.println("status is waiting");
status.wait();
}
} catch (InterruptedException e) {
System.out.println(e);
}

System.out.println("I'm at the end! Status = " + status);

}

protected void pauseApp() {
}


protected void destroyApp(boolean arg0) throws
MIDletStateChangeException {
}


public void commandAction(Command arg0, Displayable arg1) {
//this could all be ignored for the purpose of the experiment
if(arg0 == ok1){
class1.go();
myDisplay.setCurrent(form);

}else if(arg0 == ok2){
TextBox text = new TextBox("Title","Box",20,TextField.ANY);
myDisplay.setCurrent(text);
}
}
}

class Class1 extends Thread {

Command ok;
MIDlet _midlet;
Form _form;

public Class1(MIDlet mid,Form form){
_midlet = mid;
_form = form;
}

public void run(){
while(true){
try{
Thread.sleep(5000);
LCDThreads.tb.setText("1 : count = " + Thread.activeCount() + " :
" + Thread.currentThread());

Display.getDisplay(_midlet).setCurrent(_form);

}catch(Exception e){
System.out.println(e);
}
synchronized(LCDThreads.status){
System.out.println("Status is notifying");
LCDThreads.status = "11111111111111";
LCDThreads.status.notify();
}
}
}

public void go(){
synchronized(LCDThreads.tb){
LCDThreads.tb.setText("");
}
}

}
 
D

Darryl L. Pierce

Stee1HeD wrote:
protected void startApp() throws MIDletStateChangeException {
try {
synchronized(status){
System.out.println("status is waiting");
status.wait();
<snip>

Majorly bad mojo here! You're pausing the *main execution thread*. The
lifecycle methods (startApp(), pauseApp() and destroyApp()) are required to
exit as quickly as possible and should not perform any intensive or
blocking activities as they're being invoked on the main application
thread.
 
D

Darryl L. Pierce

Stee1HeD wrote:

I am trying to find a way to 'pause' a current display, call another
display with a textbox, and return to the initial display with the
contents of the textbox, like a dialog. <snip>

You don't need to pause the display, and you really *can't* do that. The
MIDP user interface model has only one Displayable ever really active. What
you should do is break the data out into an object that acts as a model,
then have your two Displayables work with it; when you want to display your
alternate Displayable to accept some input, have it write its changes to
the model object. Then, you redisplay the first Displayable and have it
read data back out from the model object.
 
S

Stee1HeD

I wrote the initial program with j2se and was trying to fit the same
interface handling into the j2me project as a part of the port. I
definitely know better now. In the future, it might be better for me
to write to the j2me standard and port to j2se if I know I would like
something on both platforms. This was my first j2me project so it was
a learning experience. :) Thanks for you help and confirmation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top