jclova
jclova

Reputation: 5576

Android: RunOnUiThread vs AsyncTask

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 performance and memory efficiency.

Example for using RunOnUithread:

    // some code #1
    Thread t = new Thread("Thread1") {
        @Override
        public void run() {
            // some code #2
            runOnUiThread(new Runnable() {
                public void run() {
                    // some code #3 (that needs to be ran in UI thread)

                }
            });

        }
    };
    t.start();

vs.

AsyncTask:

onPreExecute() {
   // some code #1
}

doInBackground() {
   // some code #2
}

onPostExecute() {
   // some code #3
}

What are the advantages / disadvantages?

Edit:

I am not looking for answers like 'easier to see the code', 'convenient for developers' etc. I am actually looking for technical diffrences behind the scene.

For example, Paul Nikonowicz's answer below would have been the answer I wanted to see. (But AsyncTask behaves the same)

Upvotes: 36

Views: 39874

Answers (5)

ahodder
ahodder

Reputation: 11439

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.

Upvotes: 2

Joon Hong
Joon Hong

Reputation: 1557

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!

Upvotes: 5

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.

Upvotes: 53

Brian Dupuis
Brian Dupuis

Reputation: 8176

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.

Upvotes: 13

Paul Nikonowicz
Paul Nikonowicz

Reputation: 3903

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.

Upvotes: 4

Related Questions