Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm wondering when should I use handler.post(runnable); and when should I use new Thread(runnable).start();

It is mentioned in developers documentation for Handler:

Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached.

Does this mean if I write in the onCreate() of Activity class:

Handler handler = new Handler();
handler.post(runnable);

then runnable will be called in a separate thread or in the Activity's thread?

Never use new Thread(runnable).start(), you can run out of VM Stack Memory Size. You want to use Executors and store a reference to that (don't create a new executor per each call that would go to a background thread) – EpicPandaForce Mar 10, 2019 at 16:38

You should use Handler.post() whenever you want to do operations on the UI thread.

So let's say you want to change a TextView's text in the callback. Because the callback is not running on the UI thread, you should use Handler.post().

In Android, as in many other UI frameworks, UI elements (widgets) can be only modified from UI thread.

Also note that the terms "UI thread" and "main thread" are often used interchangeably.

Edit: an example of the long-running task:

mHandler = new Handler();
new Thread(new Runnable() {
  @Override
  public void run () {
    // Perform long-running task here
    // (like audio buffering).
    // You may want to update a progress
    // bar every second, so use a handler:
    mHandler.post(new Runnable() {
     @Override
     public void run () {
       // make operation on the UI - for example
       // on a progress bar.
}).start();

Of course, if the task you want to perform is really long and there is a risk that user might switch to some another app in the meantime, you should consider using a Service.

But if say I want to buffer an audio stream from internet. Should I do it via handler.post or by new Thread? – reiley Feb 28, 2013 at 13:05 Really depends on your requirements. I would consider either a new thread or a Service. I'll update the answer with example in a sec. – kamituel Feb 28, 2013 at 13:07 "The explanation is wrong on many levels. – T.Dimitrov" ? Really? Absolutely unsubstantiated and useless comment. Where is your proves? – Stanly T Feb 26, 2019 at 8:25 @T.Dimitrov you might want to be more explicit about what exactly is wrong with the explanation here, otherwise no one else will know what you mean. – EpicPandaForce Mar 10, 2019 at 16:36

Does this mean if in the onCreate of Activity class I write:

Handler handler = new Handler() hanlder.post(runnable); then, runnable will be called in a separate thread or on the Activity's thread?

No it won't be. The Runnable will be called on the Main Thread itself. Handler is simply used for posting a message to the thread to which it is attached (where its is created). It does not create a thread on its own. In your example, you created a Handler in the main Thread (that where Activity.OnCreate() is called) and hence any message posted on such a Handler will be run on the Main Thread only.

You just changed completely the perfs of my app!! Moving from handler to thread just made my app flying! So big thanks man!! – hzitoun Nov 27, 2017 at 15:52

Alternatively you can skip the handler and use the post method on the view directly:

new Thread(new Runnable(){
  @Override
  public void run () {
    mUiView.post(new Runnable() {
     @Override
     public void run () {
       mUiView.setX(x);
}).start();

This is a good post that outlines the difference: What exactly does the post method do?

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.