Reputation: 3304
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.
Upvotes: 6
Views: 4550
Reputation: 386
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.
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
)
Upvotes: 0
Reputation: 33515
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.
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.)
Upvotes: 6
Reputation: 20563
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:
Upvotes: 2
Reputation: 725
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.
Upvotes: 0