difference between Thread and Handler

后端 未结 5 464
耶瑟儿~
耶瑟儿~ 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:59

    Threads:

    You can use the new Thread for long-running background tasks without impacting UI Thread. From java Thread. But here you can't update the UI from Thread.

    Since normal Thread is not much useful for Android architecture, helper classes for threading have been introduced. You can find answers to your queries on the Threading performance documentation page.

    Handler:

    This class is responsible for enqueuing any task to the message queue and processing them. Each Handler can be associated with one single thread and that thread’s message queue.

    There are two main uses for a Handler:

    • To schedule messages and runnables to be executed as some point in the future;
    • To enqueue an action to be performed on a different thread than your own.

    Looper: Looper is a worker that keep a thread alive, It loops over message queue and send the message to respective Handler.

    Message Queue: This class holds the list of messages to be dispatched by the looper. You can just call Looper.myqueue() to get list of messages. We do not normally deal with it.

提交回复
热议问题