Calling Looper more than once causes “sending message to a Handler on a dead thread”

前端 未结 2 1472
[愿得一人]
[愿得一人] 2021-01-23 10:56

I am using an Executor [fixed thread pool] with my own ThreadFactory that adds a Looper:

Handler HANDLER = new Handler();
Executor    THREADS = Executors.newFixe         


        
相关标签:
2条回答
  • 2021-01-23 11:39

    See http://code.google.com/p/android/issues/detail?id=20915, which is a possible root cause of the problem. It includes a workaround for the issue.

    0 讨论(0)
  • 2021-01-23 11:55

    If you check the source in android/os/MessageQueue.java, you can see something like the following

      if (mQuiting) {
                    RuntimeException e = new RuntimeException(
                        msg.target + " sending message to a Handler on a dead thread");
                    Log.w("MessageQueue", e.getMessage(), e);
                    return false;
                } else if (msg.target == null) {
                    mQuiting = true;
                }
       }
    

    So the message queue is basically unusable after Looper.quit() has been called the first time, as it enqueues a Message with a null target, which is the magical identifier for the message queue to stop enqueuing and appear "dead".

    0 讨论(0)
提交回复
热议问题