while圈用@N方法可以停止?? 楹文M器上可以, 在手C上s不行??

03-12-10 PJava

我在一Threade面了一x取Y料的while圈(是透^socket的方式, 用OpenInputStream, 而h端也有一VB程式在绦, 烧咦communication), 但我希望我也可以透^@socketν庀旅, 於是我再另外OpenoutputStream.

@程式在模M器上绦r]}, 但在手C上绦r, 不Ho法下命令, 而且程式W, 按Exit按o, 绦destroy()退出都不行.

了好天, 今天才想到Obreak, @x取Y料的while圈稍微绦一次就跳出圈, Y果程式在手C上绦r就可以下命令了.

但是我希望@x取Y料的while圈能一直绦, 而我也能下命令, 所以我就想在下命令之前@x取Y料的while圈停止, 下完命令之後再恿硗庖绦芯w, 榱诉_到@目的, 我O了一boolean值: breakwhile, 加在@while圈一_始的判嗍, breakwhile==falser圈才绦, 而我t在下命令之前breakwhileO成true就可以了, 下完命令恿硪绦芯w後, 在每次绦芯w一_始r, breakwhileO回false.

Y果在模M器上绦]}, 但是移到手C上绦, 整程式又W. 要如何解Q@}???

由於程式很L, 我只取其中一部分教:

class J2MEihome extends MIDlet implements Runnable {

.......

public boolean breakwhile;

public StreamConnection c2 = null;

public InputStream is = null;char ch;

String PointStatus;

..........

c2 = (StreamConnection)Connector.open("socket://mywebsite:myport");

..........

public void run() {

StringBuffer b = new StringBuffer();

breakwhile=false;

System.out.println("Thread start......");

try {

is = c2.openInputStream();

while((ch = is.read()) != -1 & breakwhile==false) {

if (ch!=13 &&ch!=10)

b.append((char) ch);

if (ch==13){

PointStatus=b.toString();

b.setLength(0);

}

//之前把 break; 在@e, 绦幸淮尉吞

}

}catch (IOException e) {}

finally {

}

} //end of run

} // end of class

而在另一旅畹某淌:

class DrawCanvas extends Canvas implements CommandListener, Runnable {

....

private J2MEihome ihomeJ;

DrawCanvas(J2MEihome ihome) { //constructor

this.ihomeJ = ihome;

}

....

public void OutPutCommand() {

PrintStream theOutputStream;

ihomeJ.breakwhile=true;

try {

String outStr="="output command";

ihomeJ.is.close();

OutputStream os2=ihomeJ.c2.openOutputStream();

os2.write(outStr.getBytes());

os2.flush();

os2.close();

new Thread(ihomeJ).start();

} //end try

catch (IOException e1) { }

} // end OutputCommand

} //end class

另外我想各楦呤: 在J2ME中的Thread ]有stop() @方法? 在Java 中能用stop() ⒁Thread 停止, J2ME 要用什N方法⒁Thread 停止??

skyleaf24
2003-12-15 15:25

]有.

但是j2me程使用的例子比^多的.

package j2me;

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

public class MyTestMIDlet
    extends MIDlet
    implements CommandListener {
  private static MyTestMIDlet _instance;
  private Display _display;
  private FirstDisplay _fd;

  public MyTestMIDlet() {
    _instance = this;
    _display = Display.getDisplay(this);
  }

  public void startApp() {
    if (_fd == null) {
      _fd = new FirstDisplay();
      _fd.addCommand(new Command("开始", Command.OK, 1));
      _fd.addCommand(new Command("退出", Command.EXIT, 2));
      _fd.setCommandListener(this);
    }
    _display.setCurrent(_fd);
  }

  public void commandAction(Command command, Displayable s) {
    String cmd = command.getLabel();
    if ("开始".equals(cmd)) {
      _fd.start();
    }
    else if
      ("退出".equals(cmd)) {
          _fd.exit();
          quitApp();
      }

  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public static void quitApp() {
    _instance.destroyApp(true);
    _instance.notifyDestroyed();
    _instance = null;
  }

}

package j2me;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class FirstDisplay
    extends GameCanvas
    implements Runnable {
  private boolean _conti = true;
  private int _rate = 5;
  private int _x = 50;
  private int _y = 10;

  public FirstDisplay() {
    super(true);
  }

  public void render(Graphics g) {
    g.setColor(255, 255, 255);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0, 0, 0);
    g.drawString("Test", _x, _y, 0);
    _x++;
    if (_x > getWidth())
      _x = 0;
    flushGraphics();
  }

  public void run() {
    Graphics g = getGraphics();
    while (_conti) {
      render(g);
      try {
        Thread.sleep(_rate);
      }
      catch (Exception e) {}
    }

  }

  public void start() {
    Thread t = new Thread(this);
    t.start();
  }

  public void exit() {
    _conti = false;
  }
}