I am currently working on an Android project which loads a lot of pictures onCreate. To make sure the UI doesn't get blocked I have the option to put it in a runnable or an AsyncTask. What is (performance wise) the better option or is it practically the same?
-
2If you create threads (or Runnables) make sure you use ThreadPools as not controlling thread creation is not a good practice. AsyncTask gives more flexibility and you can separate out the AsynTask into a separate class (generic or specific). If it is just one activity go for AsyncTask else Threads suits nicely. – drulabs May 24 '13 at 08:44
2 Answers
Yes it's more or less the same.AsyncTask
is nothing else than an convenience-class for Runnable/Thread
.
In my opinion you should use AsyncTask
. It's kind of an android standard plus it takes the task away from you to check when the work is finished. You can easily make a callback in the onPostExecute
or do whatever you want with the result. (Remeber that only doInBackGround
runs in an own thread - the other methods run in the UI thread, so you can even add dialogs there and so on if you want.

- 2,255
- 3
- 20
- 41
If you attach your Runnable
on a new Thread
then practically both would do the same under the hood. However, it is more easier to follow AsyncTask
life-cycle and its the recommended way of doing parallel things in android. Therefore, my suggestion is to go with AsyncTask
.

- 67,549
- 16
- 165
- 178