Multi-thread implementation in android Development

Source: Internet
Author: User

I. Single-threaded Model


When a program is started for the first time, Android starts a corresponding Main Thread at the same time. The Main Thread is mainly responsible for processing UI-related events, such as user key events, the user contacts screen events and Screen Drawing events, and distributes related events to corresponding components for processing. Therefore, the main thread is often called the UI thread.

When developing Android applications, you must follow the single-thread model principle: Android UI operations are not thread-safe and must be executed in the UI thread.

If you operate the UI thread directly in a non-UI thread, android is thrown. view. viewRoot $ CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views, which is different from common java programs.

Because the UI thread is responsible for event monitoring and plotting, it is necessary to ensure that the UI thread can respond to user requirements at any time, and the operations in the UI thread should be as short as the interrupt event, for time-consuming operations (such as network connections), you need to open another thread. Otherwise, if the UI thread does not respond to user requests for more than 5s, a dialog box will pop up to remind you to terminate the application.

If the UI needs to be set in the new thread, it may violate the single-thread model. Therefore, android uses a complicated Message Queue mechanism to ensure inter-thread communication.

Message Queue:
Message Queue is a Message Queue used to store messages published through Handler. Android will create an associated message queue for the UI thread by default when the program is started for the first time. myQueue () gets the message queue of the current thread, which is used to manage some upper-layer components of the program, such as activities and broadcast receivers. You can create Handler and UI thread communication in your child thread.

With Handler, you can publish or process a message or a Runnable instance. Each Handler is managed with a unique thread and the message queue of the thread.

Logoff acts as a bridge between Handler and message queue. The program component first passes the message to logoff through Handler, and logoff puts the message into the queue. Logoff also broadcasts messages in the message queue to all Handler. Handler receives the message and calls handleMessage for processing.

 

Ii. multi-thread implementation
There are several methods:

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 must be executed in the UI Thread. Therefore, you simply create a new Thread and start () this is against the Android single-threaded model. So how can we make good use of multithreading? Summary:
Event Processing Principle: all operations that may take time are processed by other threads.

For the first method:

[Java]
Activity. runOnUiThread (new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
Int I = 30;
TextView. setText ("" + I + "s ");
}
}
});

Activity. runOnUiThread (new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
Int I = 30;
TextView. setText ("" + I + "s ");
}
}
}); There are a lot of Network Information in method 2 and 3, so I will not repeat them here. The 4th methods have been introduced in the previous blog.

Iii. Summary:

1) The android system is a single-threaded system. In order to achieve the effect of multithreading, message queue is adopted.

2) To implement multithreading, runOnUiThread, post, handle, and AsyncTask can be used.

 

 

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.