First, a few concepts.
1, MessageQueue: is a data structure, is a message queue, the place to store messages. Each thread can have a maximum of one MessageQueue data structure.
When a thread is created, its MessageQueue is not created automatically. A Looper object is typically used to manage the MessageQueue of the thread. When the main thread is created, it creates a
A default Looper object, and the creation of the Looper object automatically creates a message Queue. Other non-main thread, does not automatically create looper, when needed, by adjusting the
Implemented using the Prepare function.
2. Message: The object that is stored in the messages queue. A message queue contains multiple message.
The acquisition of the message instance object, usually using the static method obtain () in the message class, has multiple overloaded versions to choose from, and it does not necessarily create a new instance directly,
Instead, see if there is an available message instance from the message pool, and there is a direct fetch to return to this instance. If there is no message instance available in the message pool,
A Message object is created with the given parameters. When Removemessages () is called, the message is removed from the message queue and placed in the message pool. Except for the above
method, you can also get a message instance from the handler object's Obtainmessage ().
3, Looper:
Is the manager of MessageQueue. Each MessageQueue can not be separated from the Looper, Looper object is created by the prepare function to achieve. At the same time, each Looper object
associated with a thread. The Looper object of the current thread can be obtained by calling Looper.mylooper ()
When you create a Looper object, a MessageQueue object is created at the same time. In addition to the default looper of the main thread, other threads do not have MessageQueue objects by default, so they cannot be
Accept the message. If you need to accept, you define a Looper object (via the Prepare function) so that the thread has its own Looper object and MessageQueue data structure.
Looper remove the message from the MessageQueue and leave it to Handler's handlemessage for processing. When processing is complete, call Message.recycle () to put it in the message pool.
The Looper.prepare () function adds Looper to threadlocal
Looper.loop (), remove the Looper from the threadlocal, and then loop through the message
4, Handler:
The processor of the message, handler, is responsible for encapsulating the information that needs to be passed into a message, by invoking the Obtainmessage () of the handler object;
The message is passed to Looper, which is implemented by the handler object's SendMessage (). The message is then placed into the MessageQueue by Looper.
When the Looper object sees that the MessageQueue contains a message, it broadcasts it. After the handler object receives the message, the Handlemessage () method of the corresponding handler object is called
To handle it.
Delivery of messages between threads
1. Handler the main thread to send itself a message
First handler himself in the main thread,
Handler bound Looper is also mainlooper,
2. Handler of other threads sends messages to the main thread
Handler on another thread
The handler bound Looper is Mainlooper
3. The main thread sends a message to another handler
Handler in the main thread
Handler bound Looper is not a thread looper
4. Handler of another thread sends a message to another thread
Handler on another thread
Handler bound Looper is not a thread looper
Code address:https://github.com/hanhan12312/HandlerLooperTest
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Interview---Handler,looper,messagequeue