如何使用非阻塞Looper / Handler实现Runnable

时间:2013-11-16 20:58:43

标签: android multithreading handler runnable looper

实现使用Handler和Looper的Runnable时,最终会在Runnable的run()方法中为Messages / Runnables设置阻塞队列,因为loop()方法会阻塞。

像这样:

public class Task1 implements Runnable {
    private Handler mHandler;
    private boolean mCancelled = false;

    private void init() {
        Looper.prepare();
        mHandler = new Handler() {
           public void handleMessage(Message msg) {
              // process incoming messages here
           }
        };
        Looper.loop();
    }

    @Override
    public void run() {
        init();

        while (!mCancelled) {
           try {
              // do stuff here
              TimeUnit.SECONDS.sleep(2);
           } catch (InterruptedException e) {
              // handle exception here
              e.printStackTrace();
           }
        }
    }

    public void cancel() {
        mCancelled = true;
        Looper.quit();
    }
}

Looper.class的实现中,您会看到正在使用的队列调用queue.next(),它可以阻塞正在运行的线程。

public static void loop() {
    // ...
    for (;;) {
        Message msg = queue.next(); // might block
        // ...
    }
}

这不是我想要的。我希望能够使用runnable任务与处理程序通信(发送和接收消息),但也可以在run()方法中执行实际工作,例如蓝牙通信。当您调用BluetoothSocket的connect()方法时,此调用将阻止当前线程,直到建立连接。 (有关详细信息,请参阅BluetoothSocket.connect()。 所以这意味着只要没有连接,我就有一个阻塞线程。没有既定的联系就没有什么可以沟通的,所以这很好。建立连接后,我想交换消息。如果线程/ runnable被取消,它也会退出Looper / Handler。

现在我的问题是:如何在Runnable中使用Looper / Handler而不让Looper阻止Runnable以便可以执行普通代码?我只想通过处理程序接收消息,而不是Runnables!

感谢您的帮助。

编辑:糟糕的是,Looper没有像BlockingQueue接口那样的函数,比如take()而不是next(),这就是阻塞。

0 个答案:

没有答案