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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值