Java Tutorial/J2ME/Thread — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 04:58, 1 июня 2010
Create a new thread
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class J2METhreadTest extends MIDlet implements CommandListener {
private Form form = new Form("Thread Test");
private StringItem text = new StringItem("Current Thread:", Thread.currentThread().getName());
private Display display;
public J2METhreadTest() {
form.append(text);
form.addCommand(new Command("Exit", Command.EXIT, 1));
form.addCommand(new Command("New Thread", Command.SCREEN, 1));
form.setCommandListener(this);
display = Display.getDisplay(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command cmd, Displayable disp) {
if (cmd.getLabel().equals("Exit")) {
notifyDestroyed();
} else {
Thread runner = new Thread(new ThreadRunner(display));
runner.start();
}
}
}
class ThreadRunner implements Runnable {
Display display;
ThreadRunner(Display display) {
this.display = display;
}
public void run() {
display.setCurrent(new Alert(Thread.currentThread().getName()));
try {
Thread.sleep(3000);
} catch (Exception e) {
}
}
}
Paint Canvas in a thread
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class J2MESweep extends MIDlet {
public void startApp() {
final SweepCanvas sweeper = new SweepCanvas();
sweeper.start();
sweeper.addCommand(new Command("Exit", Command.EXIT, 0));
sweeper.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
sweeper.stop();
notifyDestroyed();
}
});
Display.getDisplay(this).setCurrent(sweeper);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class SweepCanvas extends Canvas implements Runnable {
private boolean mTrucking;
private int mTheta = 0;
private int mBorder = 10;
private int mDelay = 50;
public void start() {
mTrucking = true;
Thread t = new Thread(this);
t.start();
}
public void stop() {
mTrucking = false;
}
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setGrayScale(255);
g.fillRect(0, 0, width - 1, height - 1);
int x = mBorder;
int y = mBorder;
int w = width - mBorder * 2;
int h = height - mBorder * 2;
for (int i = 0; i < 8; i++) {
g.setGrayScale((8 - i) * 32 - 16);
g.fillArc(x, y, w, h, mTheta + i * 10, 10);
}
}
public void run() {
while (mTrucking) {
mTheta = (mTheta + 1) % 360;
repaint();
try {
Thread.sleep(mDelay);
} catch (InterruptedException ie) {
}
}
}
}
Thread in J2ME
import java.io.IOException;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
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 {
}
}