Handler vs AsyncTask

前端 未结 8 1570
猫巷女王i
猫巷女王i 2020-11-28 18:36

I\'m confused as to when one would choose AsyncTask over a Handler. Say I have some code I want to run every n seconds which will update the UI. Why would I choose one ove

相关标签:
8条回答
  • 2020-11-28 18:38

    They are best interview question which is asked. AsyncTask - They are used to offload of UI thread and do tasks in background. Handlers - Android dosent have direct way of communication between UI and background thread. Handlers must be used to send message or runnable through the message queue.

    So AsyncTasks are used where tasks are needed to be executed in background and Handlers are used for communication between a UI and Background Thread.

    0 讨论(0)
  • 2020-11-28 18:47

    My rule of thumb would be:

    • If you are doing something isolated related to UI, for example downloading data to present in a list, go ahead and use AsyncTask.

    • If you are doing multiple repeated tasks, for example downloading multiple images which are to be displayed in ImageViews (like downloading thumbnails) upon download, use a task queue with Handler.

    0 讨论(0)
  • 2020-11-28 18:47

    AsyncTask presumes you will do something on the UI thread, after some background work is finished. Also, you can execute it only once (after this, its status is FINISHED and you'll get an exception trying to execute it once more). Also, the flexibility of using it is not much. Yes, you can use THREAD_POOL_EXECUTOR for a parallel execution, but the effort might be not worthy.

    Handler doesn't presume anything, except handling Runnables and Messages. Also, it can be run as many times as you wish. You are free to decide to which thread it must be attached to, how it communicates with other handlers, maybe produce them with HandlerThread. So, it's much more flexible and suitable for some repeated work.

    Check different kind of Handler examples here.

    0 讨论(0)
  • 2020-11-28 18:50

    Always try to avoid using AsyncTask when possible mainly for the following reasons:

    • AsyncTask is not guaranteed to run since there is a ThreadPool base and max size set by the system and if you create too much asynctask they will eventually be destroyed

    • AsyncTask can be automatically terminated, even when running, depending on the activity lifecycle and you have no control over it

    • AsyncTask methods running on the UI Thread, like onPostExecute, could be executed when the Activity it is referring to, is not visible anymore, or is possibly in a different layout state, like after an orientation change.

    In conclusion you shouldn't use the UIThread-linked methods of AsyncTask, which is its main advantage!!! Moreover you should only do non critical work on doInBackground. Read this thread for more insights on this problems:

    Is AsyncTask really conceptually flawed or am I just missing something?

    To conclude try to prefer using IntentServices, HandlerThread or ThreadPoolExecutor instead of AsyncTask when any of the above cited problems ma be a concern for you. Sure it will require more work but your application will be safer.

    0 讨论(0)
  • 2020-11-28 18:50

    If you want to do a calculation every x seconds, you should probably schedule a Runnable on a Handler (with postDelayed()) and that Runnable should start in the current UI thread. If you want to start it in another thread, use HandlerThread. AsyncTask is easier to use for us but no better than handler.

    0 讨论(0)
  • 2020-11-28 18:56

    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.

    AsyncTask provides a simple method to handle background threads in order to update the UI without blocking it by time consuming operations.

    The answer is that both can be used to update the UI from background threads, the difference would be in your execution scenario. You may consider using handler it you want to post delayed messages or send messages to the MessageQueue in a specific order.

    You may consider using AsyncTask if you want to exchange parameters (thus updating UI) between the app main thread and background thread in an easy convinient way.

    0 讨论(0)
提交回复
热议问题