After looking in-depth, it's straight forward.
AsyncTask:
It's a simple way to use a thread without knowing anything about the java thread model.
AsyncTask gives various callbacks respective to the worker thread and main thread.
Use for small waiting operations like the following:
- Fetching some data from web services and display over the layout.
- Database query.
- When you realize that running operation will never, ever be nested.
Handler:
When we install an application in android, then it creates a thread for that application called MAIN UI Thread. All activities run inside that thread. By the android single thread model rule, we can not access UI elements (bitmap, textview, etc..) directly for another thread defined inside that activity.
A Handler allows you to communicate back with the UI thread from other background threads. This is useful in android as android doesn’t allow other threads to communicate directly with UI thread. A handler can send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When a new Handler is created, it is bound to the thread/message queue of the thread that is creating it.
It's the best fit for:
- It allows you to do message queuing.
- Message scheduling.
Thread:
Now it's time to talk about the thread.
Thread is the parent of both AsyncTask and Handler. They both internally use thread, which means you can also create your own thread model like AsyncTask and Handler, but that requires a good knowledge of Java's Multi-Threading Implementation.