Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real difference between AsyncTask and Thread

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.

like image 664
Fran b Avatar asked Aug 01 '12 11:08

Fran b


People also ask

What is difference between thread and AsyncTask?

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. Long task in general.

What is the difference between handler vs AsyncTask vs thread?

Using Handlers you have the advantage of MessagingQueues , so if you want to schedule messages or update multiple UI elements or have repeating tasks. AsyncTask are similar, in fact, they make use of Handler , but doesn't run in the UI thread, so it's good for fetching data, for instance fetching web services.

What is difference between service and AsyncTask?

service is like activity long time consuming task but Async task allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.

What is difference between service and thread?

Service : is a component of android which performs long running operation in background, mostly with out having UI. Thread : is a O.S level feature that allow you to do some operation in the background.


1 Answers

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 
like image 56
Ashish Dwivedi Avatar answered Oct 18 '22 00:10

Ashish Dwivedi