Thread in J2ME : Thread « J2ME « Java Tutorial






import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;

public class J2MEBackgroundProcessing extends MIDlet implements CommandListener {
  private Display display;

  private Form form= new Form("Background Processing");

  private Command exit = new Command("Exit", Command.EXIT, 1);

  private Command start = new Command("Start", Command.SCREEN, 2);

  public J2MEBackgroundProcessing() {
    display = Display.getDisplay(this);
    form.addCommand(exit);
    form.addCommand(start);
    form.setCommandListener(this);
  }

  public void startApp() {
    display.setCurrent(form);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      destroyApp(false);
      notifyDestroyed();
    } else if (command == start) {
      Process process = new Process(this);
      process.start();
    }
  }
}

class Process implements Runnable {
  private BackgroundProcessing MIDlet;

  public Process(BackgroundProcessing MIDlet) {
    this.MIDlet = MIDlet;
  }

  public void run() {
    try {
      transmit();
    } catch (Exception error) {
      System.err.println(error.toString());
    }
  }

  public void start() {
    Thread thread = new Thread(this);
    try {
      thread.start();
    } catch (Exception error) {
    }
  }

  private void transmit() throws IOException {
  }
}








31.23.Thread
31.23.1.Create a new thread
31.23.2.Paint Canvas in a threadPaint Canvas in a thread
31.23.3.Thread in J2ME