简体   繁体   中英

Handler and interface runnable - which have new thread

I have read on stack this:

When you use new Handler().post(r) (or Message), you added the Runnable object to Looper and execute the code later in the same thread.

this answer is accepted.

So now I have a dilemma, some guys on my last interview give me tip: if you want to run something in other thread and update from this new thread UI, lets use a Handler.

So Handler is new thread or not ?
Or maybe runnable in this thread works on other thread ? Can somebody explain me ?

from official doc:

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

  • Handler is not a new thread. It is just a mechanism to schedule some task to be done in the UI thread.
  • Creating/Posting to a Handler does not create new thread.
  • The Runnable posted to a Handler runs in the UI thread as soon as UI thread becomes free.

When you create a Runnable instance and post it, its reference gets stored and its run method will be called from the UI thread, at some point in the future. (You can also specify the delay using the method postDelayed() .)

A Handler is associated with a Looper (and that Looper 's thread). When you call new Handler() to create a new handler you're associating it with the Looper for the current thread (the thread where the current code is running).

Once you have this object, you can use it from another thread to report results, etc.

So for example, from a background thread you can call post() on a handler object that is associated with the UI thread. The Runnable you pass will be executed by that handler in its thread (not in the thread where the post() function was called) when the Handler gets to it.

For another source of info/details on loopers and handlers see https://developer.android.com/training/multiple-threads/communicate-ui

Here's the quote form Android documentation :

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. 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.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

This part is quite important:

When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it

Handler is not a new thread and it can handle messages in a queue when you're passing new Runnables to the Looper thread, which process them. Each message can be processed in a separate thread or thread pool, when you explicitly do it. Otherwise, it will be processed in the thread, where Looper was created (it can be main/UI thread or other thread).

I was also confused about this mechanism some time ago, collected some links, resources and wrote sample app using Handler and Looper . You can check it here: https://github.com/pwittchen/android-looper-sample . Maybe you'll find it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM