消息传递到Java J2ME中的异步工作线程 [英] Message passing to asynchronus working thread in Java J2ME

查看:81
本文介绍了消息传递到Java J2ME中的异步工作线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究J2ME蓝牙应用程序,其中一个类正在搜索其他蓝牙设备.它在另一个线程中执行此操作,因此GUI不会冻结.

I'm working on a J2ME Bluetooth application, and one of the classes searches for other Bluetooth devices. It does this in another thread, so the GUI doesn't freeze up.

我遇到的问题是如何将消息传递给线程.我可以要求它搜索或取消搜索,它可以告诉GUI它找到了其他设备.目前,我使用通知并等待,但这似乎是一种黑客.我真正想要的是通过参数调用notify的某种方式,例如我想要它执行的操作.有什么办法吗?

The problem I have is how to pass messages to the thread. I can ask it to search, or cancel searching, and it can tell the GUI it has found some other devices. Currently I use notify and wait, but that seems like a hack. What I really want is some way of calling notify with a parameter, for example what I want it to do. Is there any way to do this?

推荐答案

解决这种情况的一般方法如下:

The general approach to this kind of situation must be as follows:

  1. 将远程数据源与视图"分离.
  2. 确保在基础数据更改时,视图能够动态更新. J2ME组件默认情况下会执行此操作-但是,如果您编写自己的组件,则必须考虑到这一点.
  3. 运行一个单独的线程并检索数据.
  4. 每当数据到达时通知视图.

MIDlet的工作代码发布在下面

The working code for the MIDlet is posted below

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 J2ME中的异步工作线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆