difference between Thread and Handler

后端 未结 5 504
耶瑟儿~
耶瑟儿~ 2020-12-23 17:08

Can somebody tell me the deference between Thread and Handler? When we use Thread and when we use Handler?

I have two code in my project , But I can\'t understand th

5条回答
  •  不知归路
    2020-12-23 18:01

    Threads are generic processing tasks that can do most things, but one thing they cannot do is update the UI.

    Handlers on the other hand are background threads that allow you to communicate with the UI thread (update the UI).

    So for example show a toast or a update a progress bar via a message (Runnable) posted to a handler but you can't if you start this runnable as a thread.

    With handler you can also have things like MessageQueuing, scheduling and repeating.

    I am yet to encounter a situation where I needed a thread in android.

    I mostly use a combination of AsyncTasks and Handlers.

    Handlers for the aforementioned tasks.

    AsyncTasks for download/ data fetching and polling etc.

提交回复
热议问题