Asynctask vs Thread in android

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 excute() method
  • Triggered from main thread
  • Runs on worker thread
  • Must be executed and created from the main thread

For long-running or CPU-intensive tasks, there are basically two ways to do this: Java threads, and Android's native AsyncTask.

Neither one is necessarily better than the other, but knowing when to use each call is essential to leveraging the system's performance to your benefit.

Use AsyncTask for:

  1. Simple network operations which do not require downloading a lot of data
  2. Disk-bound tasks that might take more than a few milliseconds

Use Java threads for:

  1. Network operations which involve moderate to large amounts of data (either uploading or downloading)
  2. High-CPU tasks which need to be run in the background
  3. Any task where you want to control the CPU usage relative to the GUI thread

And there are lot of good resources over internet which may help you:

http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html


If you use Java threads you have to handle the following requirements in your own code:

Synchronization with the main thread if you post back results to the user interface

No default for canceling the thread

No default thread pooling

No default for handling configuration changes in Android