java - Java J2ME 中消息传递到异步工作线程

标签 java multithreading java-me

我正在开发 J2ME 蓝牙应用程序,其中一个类搜索其他蓝牙设备。它在另一个线程中执行此操作,因此 GUI 不会卡住。

我遇到的问题是如何将消息传递到线程。我可以要求它搜索,或取消搜索,它可以告诉 GUI 它已经找到了一些其他设备。目前我使用通知和等待,但这看起来像是一个黑客。我真正想要的是使用参数调用通知的某种方式,例如我想要它做什么。有什么办法可以做到这一点吗?

最佳答案

处理这种情况的一般方法必须如下:

  1. 将远程数据源与“ View ”解耦。
  2. 确保 View 能够在基础数据发生变化时动态更新。 J2ME 组件默认执行此操作 - 但如果您编写自己的组件,则必须考虑到这一点。
  3. 运行单独的线程并检索数据。
  4. 每当数据到达时通知 View 。

下面发布了 MIDlet 的工作代码

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;


public class AsyncUI extends MIDlet implements SearchListener, CommandListener{
    Command CANCEL = new Command("STOP SEARCH",Command.CANCEL,1);
    Command EXIT = new Command("EXIT",Command.EXIT,2);
    SearchDevices finder = new SearchDevices();
    List deviceList = new List("List of Devices",List.IMPLICIT);

    public void startApp() {
        Display d = Display.getDisplay(this);
        finder.setSearchListener(this);
        deviceList.addCommand(CANCEL);
        deviceList.addCommand(EXIT);
        deviceList.setCommandListener(this);
        d.setCurrent(deviceList);
        new Thread( finder).start();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void found( Device d){
        deviceList.append(d.getName(), null);
    }
    public void commandAction( Command c, Displayable d){

        if( c == CANCEL){
            finder.cancel();
            deviceList.removeCommand(CANCEL);
        }else if( c== EXIT ){
            finder.cancel(); /* Cleanup all resources before you quit*/
            notifyDestroyed();
        }
    }
}

class SearchDevices implements Runnable{

    private boolean keepFinding=true;
    private static final int LONG_TIME=10000; /* 10 Seconds */
    SearchListener l =null; /* Currently only one listener. There could be many*/

    public void run(){
        int i =0;
        System.out.println(" -- Started the activity of finding --");
        while( keepFinding){
            try {
                Thread.currentThread().sleep(LONG_TIME);
                Device d = new Device("Device Found "+i);
                i++;
                System.out.println(" -- Found the device --");
                l.found(d);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println(" -- No more devices will be found  --");
    }

    public void cancel(){ keepFinding = false; }
    public void setSearchListener( SearchListener l){this.l=l;}


}

    class Device{
        String name;
        public Device(String name ){ this.name = name; }
        public String getName(){ return name ; }
    }

    interface SearchListener{
        public void found( Device device);
    }

关于java - Java J2ME 中消息传递到异步工作线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1231572/

相关文章:

java - 无法从 SpringSource Tool Suite 2.8.1 热部署到 GlassFish 3.1

c# - 停止正在运行的线程的安全方法是什么?

java - 如何在 J2ME 中解析 URL

multithreading - Swift 是否有 Java ThreadPoolExecutor 的类似物?

python - 强制 GIL 不切换线程

java - 如何找到某些手机的内存大小?

java - J2ME 文本字段异常

java - 如何使用 if 语句来更改属性的值?

java - NullPointerException:我如何给它分配一些东西,使它不为空?

java - Intellij tomee 设置 tomee.serialization.class.whitelist