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

前端 未结 2 1473
[愿得一人]
[愿得一人] 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: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".

提交回复
热议问题