Android process and thread management

Source: Internet
Author: User
Tags message queue

First, Introduction

A process is a running instance of a program that distinguishes the "program" from the static concept. Thread is the basic unit of CPU calls.

Ii. components of the process

The four components in Android are part of the process, and the Android app creates the app process when it starts, as well as the main thread (UI thread) and two binder threads. Create a new project, create an activity, Service, Broadcastreceiver in the project, respectively, in OnCreate (...) and Onreceiver (...) Method Debug Breakpoint Debugging. View activity, Service, Broadcastreceiver component creation and startup processes.

Launch app creation app process:

Activity Startup:

Service startup:

Broadcastreceiver Boot:

By debugging, you can see that the activity, Service, Broadcastreceiver components are created in the main thread, and that the creation process is basically the same. Therefore, the Service, Broadcastreceiver, executes in a child thread when the time-consuming operation is performed.

Iii. Runnable, Message, MessageQueue, Looper, Handler

In Android development, you use Message Queuing to complete inter-thread communication. The thread that uses Message Queuing is the message loop (msg looper). The message loop constantly checks the message queue for new messages. A message loop consists of a thread and a looper; the Looper object manages the thread's message queue.

The main thread of Android is also a message loop, and also has a looper, the main thread all tasks are done by Looper. A message loop consists of a thread and a looper, and the Looper object manages the thread's message queue, so looper constantly crawls the message from the message queue and finishes the task specified by the message.

PS: Thread By default, there is no message loop (Looper), in Android, only the main thread default is Looper (message loop). In the newly created thread, use Looper to create a looper first.

Learn the meanings of runnable, Message, MessageQueue, Looper, handler from the figure below:

(This image is from other book materials)

Blending this meaning together is the whole process of a message from the issue to execution:

(This image is from other book materials)

From these two diagrams above, we can see:

1. runnable and message are pressed into the MessageQueue to form a message collection.

2. Looper in the continuous cycle, and constantly doing something, such as: Looper in the MessageQueue from the non-stop to take out the item, and then passed to the handler execution.

3. Handler is constantly doing something specific, such as: handler receives a message that parses a back-end data and updates it to the UI.

From the above, handler and thread do not have a direct relationship from the table image, but because:

1. Each thread can only correspond to one looper;

2. Each looper can only correspond to one MessageQueue;

3. Each MessageQueue has n a message;

4. Specify at most one handler per message to process;

Thus, it is possible to introduce thread and handler as a one-to-many relationship. And Handler has two roles:

1. Process the message as a message processor.

2. Be responsible for pressing the message into the MessageQueue.

Iv. message and handler

Message is paired with handler, the message is messages, and handler is the object of the message to complete the task.

A message is an instance of a message that implements some instance variables of the message class at the same time,

      • What: User-defined int type message code to describe the message.
      • OBJ: The user-specified object, which is the message-passing data, that is sent with the message.
      • Target: The completion message specifies the target of the task, which is the object used to complete the message-specific task.

Handler is an object that completes a message or message assignment, and handler is not only the target or object of the completion message, but also the interface for creating and publishing messages (message).

(Figure from the Android authoritative guide)

    • The Looper has a message queue for messages, so that message must be published or read on Looper.
    • A handler is associated with only one looper, and a message is associated with only one target handler (message destination). Instead, Looper owns the entire queue of messages.

Take a look at the diagram below,

(Figure from the Android authoritative guide)

As you can see from the above figure, there are multiple handler associated with a looper, which means that one handler message is placed in the same messages queue as the other handler message.

v. Use of handler

1. Using Handler.obtainmessage (...) Method gets the message from the public loop of the messages queue.

2. Using the Handler.sendtotarget () method, the method message is sent to the handler associated with the message. The handler will place the message at the end of the Looper queue.

3. Looper in a message queue, gets a specific message and sends it to the message destination (handler associated with it) to be processed. Messages are generally in the handler (message destination) of the Handlermessage (...). The implementation method processes the message to specify the task to complete. Generally, to inherit the handler base class, overwrite the Handlermessage () method.

Vi. Transmission of handler

An instance of the handler class that can be passed to other sub-threads as a function parameter.

The main thread has a handler and Looper message loop, in which a handler is created on the main thread and automatically associated with the looper of the main thread. The handler that is created by the main thread is passed to another child thread, and the handler that is passed is always kept in contact with the looper of the thread that created it. Therefore, any outgoing handler that are responsible for processing the message will be processed in the message queue of the main thread.

Then, in this way, you can update the main thread's events, data, or UI in the child threads.

Vii. Create a looper (message loop) in a new thread

There is no need to create looper in the main thread because when the app starts, the Looper is created by default in the main thread of the creation, and there can be only one looper in the threads. There are multiple systems that will make an error.

Looper why not block the main thread, suggesting ANR (application not responding)?

Because the activity in the main thread and all events are performed through Looper. Looper has a message queue that receives event messages and executes events continuously. The blockage in this refers to the Looper loop not being clogged, not looper clogging the thread.

Android process and thread management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.