首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Kuix 课程7 Worker

2012-09-09 
Kuix 教程7 Workerorg.kalmeo.util.worker接触这个类是在Kuix的Demo中的进度条,系统提供两种实现,一种是用

Kuix 教程7 Worker

org.kalmeo.util.worker

接触这个类是在Kuix的Demo中的进度条,系统提供两种实现,一种是用Worker添加WorkerTask的,一种是自己用线程,实质都是一样的,Worker是一个线程,处理自己的任务队列,为每个任务启动一个线程,只不过线程没执行一次默认修改60ms.

public static final Worker instance = new Worker();
?Worker会自动创建一个自己的实例,Kuix 在initialize()中会启动这个线程.

KuixCanvas中(264行)会启动一个任务,用它来处理key和mouse事件

workerTask = new WorkerTask() {...}
?另外,Worker还有另外一个作用(473行),Worker.instance.isCurrentThread()用来判断当前运行线程是否是最后一个启用的线程,以此判断当前线程是否属于当前窗口.有点难以理解,我的理解是J2ME用Thread管理所有线程,而Kuix则试图用Worker.instance管理自己的线程.

/** * Revalidate (and repaint) the desktop as soon as possible. If the current * thread is the worker thread the task is done immedialty else it is * deferred to the next frame. */public void revalidateAsSoonAsPossible() {if (!Worker.instance.isCurrentThread()) {revalidateNextFrame();} else {forceRevalidate();forceRepaint();}}/** * @return <code>true</code> if the current Thread is the {@link Worker} thread */public boolean isCurrentThread() {return (Thread.currentThread() == thread);}
?

KuixMidlet甚至用它来管理Midlet的生命周期,

/* (non-Javadoc) * @see org.kalmeo.kuix.core.KuixInitializer#destroyImpl() */public void destroyImpl() {if (Worker.instance.isRunning()) {Worker.instance.pushTask(new WorkerTask() {/* (non-Javadoc) * @see org.kalmeo.util.worker.WorkerTask#run() */public boolean run() {destroyApp(false);notifyDestroyed();return true;}});} else {destroyApp(false);notifyDestroyed();}}
?

picturebox用它来展示动画(348行)

PopupBox用它来关闭splash的弹出窗口,duration是启动时设置的弹出时间.

/* (non-Javadoc) * @see org.kalmeo.kuix.widget.Widget#onAdded(org.kalmeo.kuix.widget.Widget) */protected void onAdded(Widget parent) {if (duration != -1) {Worker.instance.pushTask(new WorkerTask() {private long startTime = System.currentTimeMillis();/* (non-Javadoc) * @see org.kalmeo.kuix.core.worker.WorkerTask#execute() */public boolean run() {if ((System.currentTimeMillis() - startTime) > duration) {remove();return true;}return false;}});}}
?

Text用它来,似乎是做走马灯效果.(205行)

TextArea用它在修改文本后,调用onChange方法,不能理解,为什么不直接调用?延时?

/* (non-Javadoc) * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable) */public void commandAction(Command command, Displayable displayable) {if (command == validateCommand) {String textBoxString = textBox.getString();boolean changed = textBoxString != null && !textBoxString.equals(getText());setText(textBoxString);if (changed && onChange != null) {Worker.instance.pushTask(new WorkerTask() {public boolean run() {Kuix.callActionMethod(Kuix.parseMethod(onChange, TextArea.this));return true;}});}}Display.getDisplay(Kuix.getCanvas().getInitializer().getMIDlet()).setCurrent(Kuix.getCanvas());}
?

TextField用来弹出tip


热点排行