difference between Thread and Handler

后端 未结 5 499
耶瑟儿~
耶瑟儿~ 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 17:58

    Thread actually creates new thread - part of job running in background relatively to current thread.

    Handler itself doesn't provide any mechanisms for background job - it is just a tool to access message queue (Looper) associated with some thread. UI thread have Looper attached by default, so it is common practice to update UI with Handler.post(Runable) which means execute some piece of code on thread which is associated with this Handler.
    As soon as Handler serves Looper, it can't be created in a thread which have no associated Looper.

提交回复
热议问题