1

I am little bit confused with multithreading in Android. I am aware we can achieve using AsyncTask and Handler. Generally when should we implement by extending Thread Class in Android? Can anyone give an example that we need to do it only by extending thread class but not with AsyncTask or Handler.

  1. Consider a example app, we have a bouncing ball in an activity(forget the animation part), I need to change the color of the ball every 20 minutes, and I need to get the color code from the server and update the ball UI. Now how can I achieve this ? Can someone explain using AsyncTask or Handler and also only using Thread Class(without Asynctask or Handler)?

  2. How should I handle downloading large payloads from server using services.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
teekib
  • 2,821
  • 10
  • 39
  • 75

3 Answers3

6

Thread

  • Long task in general
  • Invoke by thread.start() method
  • Triggered from any thread
  • Runs on its own thread
  • Manual thread management/code may become difficult to read

AsyncTask

  • Small task having to communicate with main thread
  • Invoke by execute() method
  • Triggered from main thread
  • Runs on worker thread
  • Must be executed and created from the main Thread

Service

  • Task with no UI,but should not use for long Task. Use Thread within service for long Task
  • Invoke by onStartService()
  • Triggered from any Thread
  • Runs On Main Thread
  • May block main(UI) thread

IntentService

  • Long task usually no communication with main thread if communication is needed then it is done by Handler or broadcast
  • Invoke via Intent
  • Triggered from Main Thread (Intent is received on main Thread and worker thread is spawned)
  • Runs on separate thread
  • Can't run task in parallel and multiple intents are Queued on the same worker thread.
Community
  • 1
  • 1
Umang Kothari
  • 3,674
  • 27
  • 36
0

An AsyncTask is basically a thread. It has 3 callBack methods(main) and the are executed in this order top to bottom:

 1. onPreExecute()
 2. doInBackground()
 3. onPostExecute()
 4. onProgressUpdate()

The onPreExecute() and onPostExecute() are running on the UI thread and doInBackground() is a separate thread as such. If you spawn a thread(not a AsyncTask), and try to set a property of a UI element from it, this will raise an exception saying: the original thread(the UI thread) which created the view can only modify it. So only the UI thread has these two properties:

  1. Only it can add/modify the UI of the App
  2. It cannot be blocked for more than 5 secs(ANR exception)

So to over come both these limitations in one go, we have AsyncTask, it can run resource consuming operations(network access, implementing gaming logic etc) in doInBackground(), and still provide ability to alter the UI from onPreExecute() and onPreExecute().

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Parth Kapoor
  • 1,494
  • 12
  • 23
  • Hi Thanks for the answer , can give your view on Bouncing Ball App – teekib Jun 20 '14 at 11:58
  • Get all color codes before the animation starts and save them locally==>Use handler to count 20 mins==> finally get reference of your activity(this), this.runOnUIThread(new runnable())==> here just change the color of your ball. – Parth Kapoor Jun 20 '14 at 12:04
0

Thread: Use it to separate long running computation from Main Thread ( UI Thread). If you don't need to update UI or send messages back to UI Thread, use it for long running tasks. But you don't really need to extend Thread and write your logic by overriding Thread methods. But still HandlerThread is effective compared to java Thread.

AsyncTask : it is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few milli seconds at the most). Recommended for 5 milli seconds task execution.

Service: Use it to handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

HandlerThread/Handler: A HandlerThread is effectively a long-running thread that grabs work from a queue, and operates on it. You can use it even to send results back to UI Thread via Handler of UI Thread.

Have a look at below posts for more details:

Asynctask vs Thread vs Services vs Loader

Handler vs AsyncTask vs Thread

Creating Background Service in Android

Android: Toast in a thread

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211