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("");
}
}
}
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("");
}
}
}