Android: RunOnUiThread vs AsyncTask

前端 未结 5 1040
渐次进展
渐次进展 2020-12-01 00:50

I believe Google suggests developers to use AsyncTask. However, I would like to know how is it different from using \'new Thread\' and then calling \'RunOnUiThread\' in perf

相关标签:
5条回答
  • 2020-12-01 01:20

    It's a convenience, essentially. The AsyncTask framework deals with managing the Thread pool and provides a simple, understandable interface. It's well-known -- by those that know how to use AsyncTask -- that UI activity can go on in onPreExecute(), onPostExecute() and onProgressUpdate(), and that all of the "heavy lifting" is done in doInBackground() where you can't touch the UI.

    It makes it easy for a developer to have a simple background task that can easily post updates to the UI thread and return results when done. It's not magic, it's just a convenience.

    0 讨论(0)
  • 2020-12-01 01:29

    When you use new Thread you're really creating a new thread every time you execute that. AsyncTask however, uses a static pool of max 128 threads and will reuse an old thread whenever it exists. So, running AsyncTask 10 times in serial will only create one thread that runs the task 10 times instead of 10 threads.

    That's one of the differences among many.

    0 讨论(0)
  • 2020-12-01 01:31

    These are HUGELY different.

    • First ALL user interaction is done on the main thread as well as all graphics work.
    • Second AsyncTask is designed for short spurts of dedicated activity, such as downloading a file or uploading some data.
    • Third because all UI and user interactions is done in the main thread, if you start pushing stuff to this thread, the device will appear to lag and be less responsive to the user's commands.

    The only reason you want to run something in the UI thread is to interact with widgets. Other than that, if you want to do long processing, use an AsyncTask.

    Edit:

    You could download your data in a separate thread, and I have no issues with that. The problem comes when you want to update the UI. Alone, it is impossible to modify the UI from a child thread. Instead, you will have to create either a handler tied to the UI thread or create a new thread that is destined to run in the UI thread (as in your example). This is not only tedious, but a tragic waste of resources. The AsyncTask takes care of this simply and efficiently.

    To address your last point, you are correct. The AsyncTask does have access to the main thread in pre/postExecute. However, the processing (the primary source of the UI lag) that the task is preforming is not. With the Task, the UI will only be impacted based on what you are drawing, as opposed to having to wait for the Task to finish its job and whatever drawing it wants to do.

    0 讨论(0)
  • 2020-12-01 01:32

    The main disadvantage is that using your own Thread will keep your activity alive when finish is called. Android will give your activity time for all of your threads to finish. This has the effect of creating an activity leak that will eventually cause your app to run very very slow until your app is force quit from either the user or the OS.

    You can verify this by looking at your process in ADB. Even when the activity is finished you will still see it hanging there taking up resources.

    So, if you use your own thread, make sure you manage it. Or, just use the android api's. the choice is yours.

    0 讨论(0)
  • 2020-12-01 01:37

    According to this, AsyncTask is easier to use, but has some limitations like the following:

    • Core pool size and Work queue is fixed: 5 pools / 10 elements
      • it's hard coded and can't be changed
    • Thread priority is fixed to low
    • Exception handling is not as well supported as with Thread

    Also there will be another difference that I haven't figured out. You can find and inspect full source code of AsyncTask since Android is opensource :-)

    Have a good time with coding in Android!

    0 讨论(0)
提交回复
热议问题