0

AsyncTask must be created and executed from the Main thread and it runs on Worker thread. However, Main thread methods may be invoked in between to publish progress.

And what about handler?Can handler be created from any thread?And on which thread handler runs?How is handler different from asynctask in terms of threads?

What is the difference between AsyncTask and Handlers and which one would be better to use in Listview? ..Here it is mentioned-The Handler is associated with the application’s main thread. it handles and schedules messages and runnables sent from background threads to the app main thread...if handler runs on main thread then how it schedules message from background thread?

Community
  • 1
  • 1
user3684678
  • 451
  • 3
  • 7
  • 20

2 Answers2

4

From documentation:

When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

It means that everything you run using handler will run in that thread where the handler was created.

For better understanding read this article: Processes and Threads

  • Can handler be created from any thread or it is created from main thread only?And if it is created from main thread does it runs on main thread only? – user3684678 Mar 15 '16 at 11:22
  • 1
    It can be created from any thread. If you create it in the main thread than it runs in the main. – Konstantins Bogdanovs Mar 15 '16 at 11:24
  • http://stackoverflow.com/questions/8789415/what-is-the-difference-between-asynctask-and-handlers-and-which-one-would-be-bet ..Here it is mentioned-`The Handler is associated with the application’s main thread. it handles and schedules messages and runnables sent from background threads to the app main thread.`..if it runs on main thread then how it schedules message from background thread? – user3684678 Mar 15 '16 at 11:26
  • 1
    Handler is associated with the application’s main thread if it is created in the main thread. If you are not sure try to create handler not in main and then change something in layout using that handler (for example edittext.setText()). You will probably get an exception – Konstantins Bogdanovs Mar 15 '16 at 11:33
  • That means handler is not always associated with application’s main thread.Instead it is associated with the thread that created it? – user3684678 Mar 15 '16 at 11:35
  • +one to ur answer.if AsyncTask runs on worker thread then how it updates UI on main thread? – user3684678 Mar 15 '16 at 11:47
  • 1
    AsyncTask's onPreExecute()/onPostExecute() can be executed in UI thread because AsyncTask's Handler is created with main thread's looper. See https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/AsyncTask.java – Zamrony P. Juhara Mar 15 '16 at 11:55
  • @ZamronyP.Juhara That mean asynctaks onPreExecute(),onPostExecute(),onProgressUpdate() runs on main thread and doInBackground() runs on worker thread? – user3684678 Mar 15 '16 at 12:04
  • @user3684678 Read http://developer.android.com/intl/ru/reference/android/os/AsyncTask.html Part "The 4 steps" – Konstantins Bogdanovs Mar 15 '16 at 12:05
  • @KonstantinsBogdanovs I haave read it..here UI thread means main thread and background thread means worker thread? – user3684678 Mar 15 '16 at 12:08
  • @user3684678 Exactly. Main thread is the same as UI thread and backgound equals to worker – Konstantins Bogdanovs Mar 15 '16 at 12:09
1

Main thread is thread where UI gets updated. Worker thread is any threads other than main thread. Yes, handler can be created in any thread and it is associated with thread that created it. See Handler.

Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
  • if handler is created from main thread does it runs on main thread only? – user3684678 Mar 15 '16 at 11:23
  • If handler is created in main thread then it is associated with main thread message queue. – Zamrony P. Juhara Mar 15 '16 at 11:27
  • http://stackoverflow.com/questions/8789415/what-is-the-difference-between-asynctask-and-handlers-and-which-one-would-be-bet ..Here it is mentioned-`The Handler is associated with the application’s main thread. it handles and schedules messages and runnables sent from background threads to the app main thread.`..if it runs on main thread then how it schedules message from background thread? – user3684678 Mar 15 '16 at 11:27
  • Read Handler documentation as I posted. If you want to execute Runnable in main thread from background thread, then you can create Handler in main thread and pass it to background thread or create Handler inside background thread BUT using main thread's looper. Handler constructor without parameter will create Handler instance with current thread looper while constructor with Looper parameter will create Handler with supplied Looper instead of default looper. – Zamrony P. Juhara Mar 15 '16 at 11:35
  • Service is a component of android and Thread is a component of Operating System.Is Handler and AsyncTask component of Android? – user3684678 Mar 18 '16 at 08:19
  • `Handler` and `AsyncTask` both are part of Android. – Zamrony P. Juhara Mar 18 '16 at 08:31