Real difference between AsyncTask and Thread
Asked Answered
Q

6

19

I have been reading Android documentation (AsyncTask, Thread) and vogella tutorial about this matter, but I have doubts yet.

For example, I want to send a message from an Android app to a server. And I would like this process to be responsive. What should I use?

I have seen examples where they create a new Thread for not block UI, but this way we don't have the progress of process, also you have to process the response within the Thread because the run() method doesn't returning anything.

AsyncTask seems better option than Thread, but I don't know what are the consequences of using an AsyncTask instead of a Thread.

Quag answered 1/8, 2012 at 11:53 Comment(0)
F
33

Please read this blog

http://crazyaboutandroid.blogspot.in/2011/12/difference-between-android.html

and Details are:

Difference between Android Service,Thread,IntentService and AsyncTask

When to use ?

Service

   Task with no UI, but shouldn't be too long. Use threads within service for long tasks.

Thread

- Long task in general.

- For tasks in parallel use Multiple threads (traditional mechanisms)

AsyncTask

- Small task having to communicate with main thread.

- For tasks in parallel use multiple instances OR Executor 
Fillagree answered 5/10, 2013 at 12:44 Comment(5)
Where did you get that AsyncTask is for small tasks and Thread is for long tasks? Why shouldn't a person use AsyncTask for long tasks?Ovum
@BugsHappen Kindly check the following link and you will come to know why AsyncTask is recommended for small task.Turenne
Which link are you referring to?Ovum
@BugsHappen That is a very good question, technically there is no difference between an async task and a thread, only difference is that the async task can update the UI through its call back methods. So there should be no difference between an async task and a thread in terms of the length of operations that should be performed in them.Mopboard
Dead link, see here for the blog tableBathe
T
21

All other answers here are not complete, there is a big difference between AsyncTask and Thread, i.e.

Thread can be triggered from any thread, main(UI) or background; but AsyncTask must be triggered from main thread.

Also on lower API of Android(not sure, maybe API level < 11), one instance of AsyncTask can be executed only once.

For more information read Difference between Android Service, Thread, IntentService and AsyncTask

In general

Thread

  • Long task in general.

  • For tasks in parallel use Multiple threads (traditional mechanisms)

AsyncTask

  • Small task having to communicate with main thread.

  • For tasks in parallel use multiple instances OR Executor

Thinker answered 8/2, 2013 at 20:33 Comment(1)
Why should only short tasks be run in the AsyncTask? Since how the thread and async task operates are similar manner?Mopboard
S
4

in general using of 2 this features are equivalent, but AsyncTask is more simple in terms of integration with GUI

Sanches answered 1/8, 2012 at 11:58 Comment(4)
However the android documentation says 'AsyncTasks should ideally be used for short operations (a few seconds at the most.)'Quag
yeas, because of you work with GUI and user dont want to wait for a long period of time :)Sanches
But AsynTask will run like an independent thread. His work shouldn't affect to main thread (GUI). Or am I wrong?Quag
yes, you have 2 points to synchonize: onreexecute and onpostexecuteSanches
I
4

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

You can control its own functions

doInBackground(Params... params), onCancelled(), onPostExecute(Result result), onPreExecute(), nProgressUpdate(Progress... values), publishProgress(Progress... values)

Inceptive answered 1/8, 2012 at 11:59 Comment(0)
M
4
  • I would prefer to Use Async Task as it will let you know when the background process gets started and over and when can I parse the response.
  • Async has methods like onPreExecute and onPostExecute which will allow us to do tasks before and after calling the background tasks.
Moonstruck answered 1/8, 2012 at 12:0 Comment(0)
N
3

AsyncTask enables proper and easy use of the UI thread. - from Developer.

The thing is - AsyncTask is a special kind of Thread - one which is a GUI thread, it works in the background and also let's you do something with the GUI - it is basically "pre-programmed" for you with functions onPreExecute(), do inBackground(), onPostExecute().

In order to make Thread work that way, you have to write a loooot of code.

Nonnah answered 1/8, 2012 at 12:1 Comment(1)
loooot of code isn't needed.. Using handlers is very basicFrigidaire

© 2022 - 2024 — McMap. All rights reserved.