Why not do all the operations in the main thread(Android)? [closed]

这一生的挚爱 提交于 2019-12-26 08:27:02

问题


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.


回答1:


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?




回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/24339016/why-not-do-all-the-operations-in-the-main-threadandroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!