本文介绍了与Looper.getMainLooper()初始化处理程序并不消息回调响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现处理程序在不同的线程监听同一活套。

I am trying to implement Handlers listening on the same Looper from different threads.

下面我有两个处理器,一个在主线程创建的,另一个子线程,但是都被初始化监听的主要乐句。

Below I have two Handlers, one created in the main thread, another in the child thread, however both are initialized to listen on the Main Looper.

private Handler mMain;
public static final ThreadPoolExecutor tpe =
        (ThreadPoolExecutor) Executors.newCachedThreadPool();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMain = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            Log.wtf("", "main:" + msg);
        }
    };

    tpe.execute(new Runnable() {
        private Handler tChild = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                Log.wtf("", "child:" + msg);
            }
        };

        @Override
        public void run() {
            Log.wtf("", "send msg to main looper");
            tChild.sendEmptyMessage(100);
        }
    });
}

但是,当我发送邮件如下图所示,只有孩子处理打印消息。主要的处理程序不会收到该邮件。

But when I send a message like below, only the child handler prints the message. The main handler does not receive the message.

03-20 22:02:26.754: A/(12857): send msg to main looper
03-20 22:02:26.847: A/(12857): child:{ what=100 when=-8ms }

我是什么做错了吗?感谢您的阅读。

What am I doing wrong? Thank you for reading.

推荐答案

每个处理程序实例控制消息目标有没有办法让他们分享,让每一个消息或交送到处理程序仅由实例执行。

Each Handler instance controls the Message target and there is no way to get them to share, so every message or post sent to a Handler is only executed by that instance.

尺蠖表示的哪个线程消息/发送将被执行的可运行。在您的code,这两种处理程序将执行的handleMessage()在主线程中,尽管在单独的线程创建。这是你可以通过一个尺蠖实例给处理程序 ...如果你没有传递<$ C的真正原因$ C>尺蠖,,然后处理程序将在创建它的线程上执行code(其中的必须也可以是尺蠖线程)。

The Looper indicates which thread the messages/runnables sent will be executed on. In your code, both Handlers will execute handleMessage() on the main thread, despite being created on separate threads. That is the real reason you can pass a Looper instance to a Handler...if you pass no Looper, then the Handler will execute code on the thread in which it was created (which must also be a Looper thread).

此外,因为这个没有必要创建多个处理程序以这种方式发布的数据。一个处理程序被设计为在多线程发送的消息,而且都是在的MessageQueue 序列化和执行上所选择的尺蠖线程。您可以直接从后台线程发布到 mMain 来在该线程中执行code。在这种情况下,通过了尺蠖是多余的那个code已经在主线程。

Furthermore, because of this there isn't reason to create multiple Handlers to post data in this manner. A single Handler is designed to be sent messages from multiple threads, and they are all serialized in a MessageQueue and executed on the chosen Looper thread. You can post directly to mMain from the background thread to execute code on that thread. In this case, passing the Looper is redundant at that code is already on the main thread.

这篇关于与Looper.getMainLooper()初始化处理程序并不消息回调响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:31