What's HandlerThread?

Copied from https://developer.android.com/reference/android/os/HandlerThread.html

HandlerThread 是一个内置消息队列的线程,包含一个 Thread、一个 Looper、一个 MessageQueue。 HandlerThread is a thread with a message queue that incorporates a Thread, a Looper, and a MessageQueue. It is constructed and started in the same way as a Thread. Once it is started, HandlerThread sets up queuing through a Looper and MessageQueue and then waits for incoming messages to process:

只有一个 queue,所以可以保证顺序执行、线程安全。 There is only one queue to store messages, so execution is guaranteed to be sequential —and therefore thread safe—but with potentially low throughput, because tasks can be delayed in the queue.

    HandlerThread handlerThread = new HandlerThread("HandlerThread");
    handlerThread.start();
    mHandler = new Handler(handlerThread.getLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // Process messages here
        }
    };