5

In my app i have buttons, when clicked will query the database and show result on screen. The query action will normally take 1 ~ 3 sec. These buttons will be clicked very often.

I've implemented this action on both AsyncTask and Thread but see very little different.

However in the long term, especially when the buttons are clicked many times, which will be more beneficial, in terms of resources (CPU, memory) ?

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
Tran Ngu Dang
  • 2,540
  • 6
  • 29
  • 38
  • AsyncTaskloader is my guess: http://stackoverflow.com/a/20279805/2413303 – EpicPandaForce Mar 07 '15 at 10:14
  • Thanks for your replay EpicPandaForce. I read the link. They said: "the AsyncTaskLoader can survive through config changes like screen flips". But the query is short, is it necessary to handle the config changes also ? – Tran Ngu Dang Mar 07 '15 at 10:23
  • @ngubk, it is necessary to handle config changes in almost all cases – localhost Mar 07 '15 at 11:16

2 Answers2

6

When you use a Thread, you have to update the result on the main thread using the runOnUiThread() method, while an AsyncTask has the onPostExecute() method which automatically executes on the main thread after doInBackground() returns.

While there is no significant difference between these two in terms of "which is more beneficial", I think that the AsyncTask abstraction was devised so that a programmer doesn't have to synchronize the UI & worker threads. When using a Thread, it may not always be as simple as calling runOnUiThread(); it can get very tricky very fast. So if I were you, I'd stick to using AsyncTask and keep Thread for more specialized situations.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Thanks for your reply. But in my case, I use handler.sendMessage() in the thread instead of runOnUiThread(), is this good or AsyncTask is still better ? – Tran Ngu Dang Mar 07 '15 at 10:28
  • My implementation is very similar to this answer: http://stackoverflow.com/questions/13954611/android-when-should-i-use-a-handler-and-when-should-i-use-a-thread – Tran Ngu Dang Mar 07 '15 at 10:33
  • 1
    I understand ... what I can say is that `AsyncTask` is specifically meant for the case when one of the two threads is the UI / main thread. `Thread` and `Handler` can even be used when neither of the two threads is the UI thread. I hope you understand this difference ... thats why whenever UI is involved, one should preferably use an `AsyncTask` as that is a recognizable idiom :) ... you can of course continue to use `Thread` and `Handler` as it is equally right too. – Yash Sampat Mar 07 '15 at 10:40
  • 1
    Thanks for your answer. Then I ll use the AsyncTask, as it is a good practise – Tran Ngu Dang Mar 07 '15 at 10:45
1

An AsyncTask is used to do some background computation and publish the result to the UI thread (with optional progress updates). Since you're not concerned with UI, then a Handler or Thread seems more appropriate.

Reference : Handler vs AsyncTask vs Thread

Community
  • 1
  • 1
YFeizi
  • 1,498
  • 3
  • 28
  • 50