Understanding and analysis of single-threaded model in Android programming _android

Source: Internet
Author: User

This paper describes the understanding and analysis of the single threaded model in Android programming. Share to everyone for your reference, specific as follows:

When an Android program is started, the Android system initiates a corresponding main thread (main thread) at the same time.

Since the main task of this thread (main thread) is to handle UI-related events (such as displaying text, handling click events, displaying pictures, etc.), the system's invocation of each component is distributed from the main thread, and is often called the UI thread.

The core principle of the Imp,android single-threaded model is that the UI can only be processed in the UI thread (Main thread).
to improve Performance,android's correlation method for UI processing is not synchronized, the following exception is thrown when you try to manipulate the UI with other threads:

Android.view.viewroot$calledfromwrongthreadexception:only the original thread that created a view hierarchy can touch its Views.

Of course, this does not encourage us to put everything into the UI thread.

Some very time-consuming operations (such as network operations, database operations, and so on) that have little impact on UI updates, if placed together in a UI thread, cause UI performance to be very poor, very poor, and even pop up ANR (application not Responding) window, which is undoubtedly very unfriendly to the user.

Ps: As far as I know, the Android SDK does not support direct network operations in main thread after version 4.0, unless you have the audacity to add the following code to the main thread:

Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder (). Detectdiskreads (). Detectdiskwrites (). 
  Detectnetwork () 
  . Penaltylog (). build ()); 
Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder (). Detectleakedsqlliteobjects (). Penaltylog (). 
  Penaltydeath () 
  . Build ());

As a result, the principles of the Android single-threaded model are summed up in roughly two points:

1. You can only work with the UI in the UI thread (Main thread) and do not access the Android UI outside of the UI Threads Toolkit

2. Do not block UI threads with time-consuming operations

You can refer to the Android official training about how to handle multiple threads in Android programs

I hope this article will help you with the Android program.

Related Article

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.