高级用户界面与多线程
LCDUI 高级 LCDUI
LCDUI 与多线程 多线程在手机编程中尤为重要 手机游戏开发中, 通常由游戏主线程负责游戏的基本控制, 除此之外还会由一个或者多个线程用于播放背景音乐或声效 网络接受数据时, 传输速度通常都比较慢, 因此可以使用一个独立的线程负责接收数据, 另外一个线程负责 UI 界面和与用户的交互
LCDUI 与多线程 J2ME 是通过线程体来实现多线程的 线程体就是用于实现线程活动的主体, 由 run() 方法来实现 实现线程体的方法主要有 : (1) 通过继承 Thread 类构造线程体 (2) 通过实现 Runnable 接口构造线程体
LCDUI 与多线程 Thread 类 除了实现 Runnable 接口外,CLDC 包中还提供了通过继承 Thread 类来实现线程的方法, Thread 类直接继承了 Object 类, 可以用来实现一个具体的线程 构造方法 Thread(); Thread(String name); Thread(Runnable target); Thread(Runnable target, String name);
LCDUI 与多线程 Thread 类 主要方法 start(), 启动线程并转入就绪状态 run() 定义线程被调度之后所执行的动作, 系统自动调用, 用户不得引用, 由于 Thread 类的 run() 方法没有具体的内容, 所以用户需要创建 Thread 类的子类, 并定义新的 run() 方法 sleep(): 通常在 run() 方法中调用 sleep() 方法使自己放弃 CPU 资源 要停止线程往往通过标识符实现
LCDUI 与多线程 Thread 类 import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class mymidlet extends MIDlet{. public void startapp(){. ThreadDemo th = new ThreadDemo(); th.start();. // 添加类中类 public class ThreadDemo extends Thread{ public void run() {
LCDUI 与多线程 Runnable 接口 创建一个线程时必须实现 Runnable 接口, Runnable 接口为实现多线程提供了通用的协议, 使得对象被激活时可自动执行特定代码 实现 Runnable 接口必须实现其中的 run() 方法,run() 方法是线程的主体,Run() 方法中通常使用 sleep() 方法使线程 适当 休息, 为其它线程让出 CPU 资源
LCDUI 与多线程 Runnable 接口 import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class mymidlet extends MIDlet{. public void startapp(){. Thread th = new Thread(new RunnableDemo()); th.start();. // 添加类中类 public class RunnableDemo implements Runnable{ public void run() {
LCDUI 与多线程 Runnable 接口 import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class mymidlet extends MIDlet implements Runnable{. public void startapp(){. Thread th = new Thread(this); th.start();. // 重写 run() public void run() {
实例 1 在控制台循环打印字符串 hello 和 others
实例 1 public class mymidlet extends MIDlet { private Display dis; private Form fm = new Form("test"); public void startapp() { dis = Display.getDisplay(this); dis.setcurrent(fm); mythread mt1 = new mythread(); Thread mt2 = new Thread(new myrunable()); mt1.start(); mt2.start();
实例 1 public class mythread extends Thread { public void run(){ while(true){ System.out.println("hello"); Thread.sleep(1000); public class myrunnable implements Runnable { public void run(){ while(true){ System.out.println( others"); Thread.sleep(1000);
实例 2 实例 2: 界面上有 1 个 StringItem, 要求它上面的数字每隔 1 秒钟变化
public class MidletString extends MIDlet { private Display dis; private Form fm = new Form("String test"); private StringItem si = new StringItem("String1",""); int i=0; public void startapp() { dis = Display.getDisplay(this); dis.setcurrent(fm); fm.append(si); mythread th = new mythread(); th.start(); class mythread extends Thread{ public void run(){ while(true){ si.settext(integer.tostring(i)); i++; try{ Thread.sleep(1000); catch(exception ex){
实例 3 实例 3: 界面上有 2 个 StringItem, 要求第一个上面的数字每隔 1 秒钟变化, 第二个上面的数字每隔 2 秒钟变化
public class MidletString extends MIDlet { private Display dis; private Form fm = new Form("String test"); private StringItem si1 = new StringItem("String1",""); private StringItem si2 = new StringItem("String2",""); public void startapp() { dis = Display.getDisplay(this); dis.setcurrent(fm); fm.append(si1); fm.append(si2); mythread1 th1 = new mythread1(); th1.start(); Thread th2 = new Thread(new mythread2()); th2.start(); System.out.println("other things");
class mythread1 extends Thread{ private int i=0; public void run(){ while(true){ si1.settext(integer.tostring(i)); i++; try{ Thread.sleep(1000); catch(exception ex){ class mythread2 implements Runnable{ private int i=0; public void run(){ while(true){ si2.settext(integer.tostring(i)); i++; try{ Thread.sleep(2000); catch(exception ex){
实例 4 秒表
public class midlet2 extends MIDlet implements CommandListener { private Display dis; private Form frm; private Command exit,ok,exitwatch,start,stop; private Watch watch; public midlet2() { frm=new Form(" 秒表 "); exit=new Command("exit",Command.EXIT,1); ok=new Command("ok",Command.OK,1); exitwatch=new Command("exitwatch",Command.CANCEL,1); start=new Command("start",Command.OK,1); stop=new Command("stop",Command.STOP,1); protected void startapp(){ dis=display.getdisplay(this); dis.setcurrent(frm); frm.addcommand(exit); frm.addcommand(ok); frm.setcommandlistener(this); frm.append(" 争分夺秒 ");
public void commandaction(command c, Displayable d){ String str=c.getlabel(); if(str.equals("ok")){ watch=new Watch(); watch.addcommand(exitwatch); watch.addcommand(start); watch.setcommandlistener(this); dis.setcurrent(watch); else if(str.equals("start")){ watch.removecommand(start); watch.addcommand(stop); watch.startwatch(); else if(str.equals("stop")){ watch.removecommand(stop); watch.addcommand(start); watch.stop(); else if(str.equals("exitwatch")){ dis.setcurrent(frm); else if(str.equals("exit")){ this.notifydestroyed();
public class Watch extends Form implements Runnable { private TextField text; private boolean flag; private int count=-1; public Watch() { super(""); flag=true; text=new TextField("","",50,TextField.UNEDITABLE); this.append(text); public void startwatch(){ flag=true; text.setstring(""); Thread th = new Thread(this); th.start(); public void stop(){ flag=false;
public void run(){ while(true){ if(flag){ count++; try{ int ms=count%10; int s=count/10%60; int m = s/60%60; int h =m/60; text.setstring(h+""+m+""+s+""+ms); Thread.sleep(100);catch(Exception e){ else break;