From the docs:
When an application is launched, the system creates a thread of execution for the application, called “main.” This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events.
In other words, ALL UI-related tasks occur on the same thread, and it is called the main thread or UI thread.
When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI.
Simply put, when the UI has to wait for some I/O, data retrieval or data processing to complete before it can finish rendering the screen, your application “hangs” till that process is complete.
The user might then decide to quit your application and uninstall it if they are unhappy.
I think this is self-explanatory.
There are simply two rules to Android’s single thread model:
- Do not block the UI thread
- Do not access the Android UI toolkit from outside the UI thread
Hence, everything not related to the UI MUST be done in a separate thread, and everything related to the UI MUST be done WITHOUT any kind of parallel processing.
So how do we perform parallel processing ? Android offers different kinds of parallel processing based on your needs:
1. Good old Java Threads
2. Handlers
3. Activity.runOnUiThread(Runnable)
5. View.postDelayed(Runnable, long)
6. AsyncTasks
7. IntentServices
(Services
DO NOT run on a separate thread).
On a side note, Android specifically enforces that all network operations be performed on a separate thread, else a NetworkOnMainThreadException
is thrown.
Now coming back to your question :
My question is how is this difference form just running everything on the main thread? I know that onPostExecute has to wait for the xml do in background retrieve to finish which will still lock up the ui for the user?
What happens is that if the device has more than one core, UI rendering is done through one core (the main thread) while the doInBackground()
method is executed on the other core (the AsyncTask
thread), and the onPostExecute()
method is called on the main thread only after doInBackground()
has returned. This means that onPostExecute()
is also waiting for the UI to finish rendering, as both UI rendering & onPostExecute()
occur on the main thread.
Now if the device has only one core, everything will happen on a single core, but doInBackground()
will be executed on a separate thread, and onPostExecute()
will be called only after UI rendering is complete and doInBackground()
has returned.
I hope this was helpful.
Further Reading:
1. Understanding AsyncTask – Once and Forever
2. Is Android’s AsyncTask executing tasks serially or concurrently?
3
solved Why not do all the operations in the main thread(Android)? [closed]