-2

This is a clarifying question regarding the AsyncTask class and a specific example of using the class to do a networking operation(grabbing data).

How is the onPostExecute method running synchronously after the doInBackground operation any different than having the main thread do all the work(onPostExecute and doInBackground)?

That main thread would do these operations sequentially, grabbing network data an then performing the work in onPostExecute.

committedandroider
  • 8,711
  • 14
  • 71
  • 126
  • check this out--> http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html – Aradhna Jun 21 '14 at 06:36
  • the problem with the question is that it is too broad and not focused at all. i can't re open it for that, but i can suggest that you take a specific example that uses the onPostExecute method and you didn't understand it. sorry i couldn't be of any more help to you – No Idea For Name Nov 17 '14 at 09:33
  • @aradhna thanks for the article! This quote itself from that article answers the question - "If you perform a long lasting operation, for example accessing data from the Internet, the application blocks until the corresponding operation has finished." – committedandroider Aug 22 '15 at 18:14

3 Answers3

2

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:

  1. Do not block the UI thread
  2. 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)

4. View.post(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?

A.L
  • 10,259
  • 10
  • 67
  • 98
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Sorry I should have been more clear... the term "core" refers to the number of CPUs' in the device's microprocessor. In the past, microprocessors were single-core, and traditionally one core can execute one instruction at a time, which is to say one core handles one thread, with *context-switching* creating the illusion of multi-tasking. The existence of multiple cores in a single processor is what allows *true* multi-tasking in computers (and now mobile devices as well!) ... is this helpful ? – Yash Sampat Jun 22 '14 at 09:52
  • does a device have a microprocessor and a main processor(CPU?) – committedandroider Nov 11 '14 at 16:43
  • both refer to the same thing – Yash Sampat Nov 14 '14 at 15:15
1

It's not about efficiency: network I/O is explicitly prohibited in the main thread (at least, starting with Android 3.0). Thus, some way for offloading these tasks into a background thread is necessary.

AsyncTask is just a very convenient mechanism for achieving this.

See How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
matiash
  • 54,791
  • 16
  • 125
  • 154
0

Thats is simple. If you download your stuff on main thread, it will block your UI and ANR dialog can show up. Your Activites are tied to XML:

 setContentView(R.layout.activity_main);

But if you use Asynctask, new thread will be started, which is not related to UI. So after you start your Asynctask, you can still show anything you want, buttons will be clickable, etc. If you would download it on your main thread, user can think the application froze and quit, altrough it was just doing some calculations.

Vojtěch Pešek
  • 1,070
  • 8
  • 25