UI thread and Android single-thread model principles
When the application starts, the system createsMain thread).
This main thread is responsible for distributing events (including drawing events) to the UI components. In this main thread, your applications and AndroidUI components (components from the Android UI toolkit (components from the android. widget and android. view packages ))Interaction occurs.
SoMain threadAlso calledUI threadThat isUI thread.
The system does not create threads for each component. The UI components in the same process are instantiated in the UI thread,The system distributes calls to each component from the UI thread..
The result is that the methods for responding to system callbacks (such as onKeyDown () for responding to user actions and various lifecycle callbacks) are always run in the UI thread.
When the App performs some intensive work, the performance of the single-thread model will be very poor unless you implement it reasonably.
In particular, if all the work is in the UI thread and some time-consuming work such as accessing the network or querying the databaseBlocking UI threads,Cause the event to stop distributing(Including drawing events ). For the user, the application looks stuck. Worse, if the UI thread blocked takes too long (about 5 seconds), the user will seeANR(Application not responding) dialog box.
In addition, the Andoid UI toolkit is NOT thread-safe, so you cannot manipulate the UI components from non-UI threads. You must put all the UI operations in the UI thread, soAndroid single-threaded model has two principles:
1. Do not block the UI thread.
2. Do not access Android UI toolkit outside the UI thread(Mainly components in these two packages:android.widget
And android. view ).
Use Worker thread
According to the two principles of the single-thread model, first of all, to ensure application responsiveness, you must not block the UI thread. Therefore, when your operations are not real-time (not instantaneous ), you should put them in a single thread (calledBackgroundOrWorker thread).
For example, after clicking the button, download an image and display it in ImageView:
public void onClick(View v) { new Thread(new Runnable() { public void run() { Bitmap b = loadImageFromNetwork("http://example.com/image.png"); mImageView.setImageBitmap(b); } }).start();}
This Code uses a new thread to process network operations, but it violates the second principle:
Do not access the Android UI toolkit from outside the UI thread.
Access the UI component from a non-UI threadLeading to undefined and unpredictable behavior.
To solve this problem, Android provides some methods to access the UI thread from other threads:
- Activity. runOnUiThread (Runnable)
- View. post (Runnable)
- View. postDelayed (Runnable, long)
For example, the above Code can be changed as follows:
public void onClick(View v) { new Thread(new Runnable() { public void run() { final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png"); mImageView.post(new Runnable() { public void run() { mImageView.setImageBitmap(bitmap); } }); } }).start();}
After this change, it is thread-safe.
However, when operations become complex, the code will become very complex. To handle more complex interactions between non-UI threads and UI threads, you can consider usingHandler
To process messages in the UI thread.
You can also inherit this class AsyncTask.
Communicating with the UI Thread
Only objects in the UI thread can operate on objects in the UI thread.To transmit data from non-UI threads to the UI thread, you can run a Handler in the UI thread.
Handler is the part of the management thread in the Android framework. A Handler object is responsible for receiving and processing messages.
You can create a Handler for a new thread, or create a Handler and connect it to an existing thread.
If you connect a Handler to your UI thread, the code that processes the message will be executed in the UI thread.
You can instantiate the Handler object in the constructor of the class you created in the thread pool, and then store this object with global variables.
To connect to the UI thread, useHandler(Looper)
This constructor.
This constructor usesLooper
Object, which is another part of the framework managed by threads in the Android system.
When you use a specificLooper
When an instance creates a Handler, the Handler runs in thisLooper
.
In Handler, OverwritehandleMessage()
Method. Android systemThis method will be called when the corresponding thread managed by Handler receives a new message.
All Handler objects in a specific thread will receive the same method. (This is a one-to-many relationship ).
References
Official Training: communicates with the UI thread:
Http://developer.android.com/training/multiple-threads/communicate-ui.html
Guides: Processes and Threads
Http://developer.android.com/guide/components/processes-and-threads.html
Class reference:
Http://developer.android.com/reference/android/ OS /Looper.html
Http://developer.android.com/reference/android/ OS /Handler.html
Http://developer.android.com/reference/android/ OS /HandlerThread.html
Blog:
Android threads are used to update the UI-Thread, Handler, logoff, and TimerTask:
Http://www.cnblogs.com/playing/archive/2011/03/24/1993583.html