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.