6

In my app, I have to call a method which does some heavy work (I can feel device lagging). To avoid this I created an AsyncTask and it works perfectly fine.

I implemented the same thing using a Thread and here, too, it does not give any hiccup and works fine.

Now my question is which one better performance-wise - AsyncTask or Thread.

I know AsyncTask uses a threadpool to perform background tasks but in my case it will be called only once. So I don't think it will create any problems.

Can someone throw some light on it. Which one should I use for better performance?

Note: Both are being called in my Activity e.g. from UI the thread.

Shade
  • 9,936
  • 5
  • 60
  • 85
AndroDev
  • 3,236
  • 8
  • 35
  • 49
  • There is unlikely to be a significant performance difference between the two. `AsyncTask`s are there to allow effective multithreading on the UI thread, whereas plain threads are not allowed to make changes to the UI. This is the main difference between the two, aside from `AsyncTask`'s usage of thread pools (which, as you say, doesn't affect you) – HXCaine Apr 01 '13 at 14:12
  • This has also been discussed in this question: http://stackoverflow.com/questions/9296539/android-runonuithread-vs-asynctask – HXCaine Apr 01 '13 at 14:15
  • Thank you HXCaine for your comments. As the answer of above link says, "You are creating a new thread every time you are creating that". True. But in my case, I am calling this only once. So it does not satisfy my question. – AndroDev Apr 01 '13 at 14:22
  • 1
    Possible duplicate of [Asynctask vs Thread in android](http://stackoverflow.com/questions/18480206/asynctask-vs-thread-in-android) – Ankit Mundada Oct 21 '16 at 10:03

4 Answers4

6

Can someone throw some light on it. Which one should I use for better performance?

I think if you imagine case when you start once native Thread and AsyncTask i think that performance won't differ.

Usually native threads are used in the case if you don't want to inform potential USER with relevant information about progress in some task via UI. Here, native threads fail because they are not synchronized with UI thread and you cannot perform manipulating with UI from them.

On the other hand, AsyncTask combines background work with UI work and offers methods which are synchronized with UI and allow performing UI updates whenever you want via invoking proper methods of its lifecycle.

Generally if some task lasts more than 5 seconds you should inform USER that

"something working on the background, please wait until it will be finished"

For sure, this can be reached with both in different ways but this strongly depends on character of your task - if you need to show progress of task(how much MB is already downloaded, copying number of files and show name of each in progress dialog etc.) or you don't(creating some big data structure in "silent" only with start and end message for instance).

So and at the end of my asnwer:

Which one should I use for better performance?

Completely right answer i think you cannot get because each developer has different experiences, different coding style. How i mentioned, their performance not differ. I think that it's same(if you will read 50 MB file, it won't be faster read neither native thread nor AsyncTask). It depends again on character of task and your personal choice.

Update:

For tasks that can last much longer periods of time, you can try to think also about API tools provided by java.util.concurrent package(ThreadPoolExecutor, FutureTask etc.)

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • Nice explanation. In that case, I can say that if I dont want to update UI after my heavy work is done (provided only one task is there), I should use Thread because of ease of use. Performancewise for single task, both behaves equally. Correct me if I am wrong. – AndroDev Apr 02 '13 at 08:02
2

Async tasks are also threads. But they have some utility methods that make it very easy to small background tasks and get back to the UI to make changes to it. The performance would depend on your specific use case. Making absolute statements as to which one is always better would be simplistic and meaningless.

Note that the main advantage of Async tasks over threads is that Async tasks provide helper methods such as onPreExecute(), doInBackground(), onProgressUpdate() and onPostExecute() which make it very easy to perform short background tasks while also interacting with the UI (such as updating a progress bar). These kinds of methods are not available in generic Threads. Basically, Async tasks are threads with UI interaction component built in. Yes, you can use workarounds to try and update the UI from regular threads as well but Async tasks have been specifically built for this purpose and you don't have to deal with Context leaks and so on if you follow it's abstractions.

Async tasks are created to make developers' lives easier.

To sum up:

  • Async tasks are also threads
  • Async tasks make it easy to interact with UI while doing short background tasks
  • Neither is inherently more efficient. It depends on what you want to do.
  • Good rule of thumb: Use Async tasks if you need to get back to/update the UI after you are done with your background task. Use a regular thread if you don't.
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0

The most common use of Thread are short-term tasks because they need a lot of power and tend to drain the battery and heat the phone up.
And the common use of AsyncTasks are lengthy tasks because of the same battery drain.

But a Thread is far more powerfull, because an AsyncTasks internally uses a Thread itself, but you don't have to configure that much.

Victor
  • 706
  • 2
  • 7
  • 20
  • 3
    No. Async tasks are meant to be used for short intervals. [Quoting the docs](http://developer.android.com/reference/android/os/AsyncTask.html) - " AsyncTasks should ideally be used for short operations (a few seconds at the most.)". Battery drain is not related to the use of AsyncTasks / Threads either. – Deepak Bala Apr 01 '13 at 14:19
0

ASYNC TASK and Thread do the same thing, The difference is that you have more control on bare thread and you can benefit from the power of CPU in terms of complex implementation, the velocity of performance depends on your approach on how you implement the threading. Depending on this article I can say that asynchronous multithreading is the fastest way to perform complex tasks.

https://blog.devgenius.io/multi-threading-vs-asynchronous-programming-what-is-the-difference-3ebfe1179a5

Regarding showing updates to user on UI thread, you can do that by posting on UI from the background thread (check UIhandler and View.Post)

Nadin Martini
  • 193
  • 2
  • 4
  • 13