Overview of the Multi-threading implementation approach for Android development

Source: Internet
Author: User

One, single-threading model

When a program starts for the first time, Android initiates a corresponding main thread, which is primarily responsible for handling UI-related events such as user keystroke events, user-touching screen events, and screen drawing events, and distributing related events to the corresponding components for processing. So the main thread is often called the UI thread.

The principle of a single-threaded model must be adhered to when developing Android apps: Android UI operations are not thread-safe and must be executed in the UI thread.

If you manipulate the UI thread directly in a non-UI thread, you will throw android.view.viewroot$calledfromwrongthreadexception:only the original thread that created a View hierarchy can touch its views, which differs from normal Java programs.

Because the UI thread is responsible for the listener and drawing of the event, it is important to ensure that the UI thread is able to respond to the user's needs at any time, that the UI line thread action should be as short as the interrupt event, that the time-consuming operation (such as a network connection) requires another thread, otherwise, if the UI thread exceeds 5s, A dialog box will pop up to remind the user to terminate the application.

If you need to set the UI in a newly opened thread, you might violate the single-threaded model, so Android uses a complex message queue mechanism to ensure inter-thread communication.

Message Queue:

The message queue is an array of messages used to store messages published through Handler. Android will create an associated message queue for the UI thread by default when it starts the program for the first time, and it can get Message Queuing for the current thread through Looper.myqueue (), which manages some of the upper-level components of the program Activities,broadcast Receivers and so on. You can create handler and UI thread communication in your own child threads.

With handler you can publish or process a message or an instance of a runnable. Each handler is managed with a unique thread and Message Queuing for that thread.

Looper plays a role in communicating bridges between handler and message queues. The program component first passes the message to Looper,looper via handler and puts the message into the queue. Looper also broadcasts messages in the message queue to all Handler,handler to receive the message and call handlemessage for processing.

(above transferred from http://www.cnblogs.com/nio-nio/archive/2012/07/23/2604900.html)

Second, the realization of multithreading

There are several ways to do this:

1) activity.runonuithread (Runnable)

2) View.post (Runnable); View.postdelay (Runnable, long)

3) Handler

4) Asynctask

Android is a single-threaded model, which means that Android UI operations are not thread-safe and that these operations must be performed in the UI thread, so your simple new thread and start () will not work because it violates the Android single-threaded model. So how to use good multithreading? To summarize:
The principle of event handling: all possible time-consuming operations are placed on other threads to handle.

For the first method:

Activity.runonuithread (New Runnable () {                           @Override public             void Run () {                  //TODO auto-generated method stub              int i =;            Textview.settext ("+i+" S ");}            }          );  

2nd, 3 kinds of methods on the network more information, no longer repeat. The 4th method is described in the previous blog post.

Three, Summary:

1) Android system is a single-threaded system, in order to achieve multi-threading effect, the use of the message queue.

2) in order to realize multi-threading, it can be implemented by Runonuithread,post,handle,asynctask technology.

Overview of the Multi-threading implementation approach for Android development

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.