Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use handler.post() & when to new Thread()

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?

like image 705
reiley Avatar asked Feb 28 '13 13:02

reiley


People also ask

What does handler Post do?

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.

What is the difference between handler and HandlerThread?

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).

Does handler run on the main thread?

Short answer: they all run on the same thread. If instantiated from an Activity lifecycle callback, they all run on the main UI thread.

What is handler in Java?

A Handler object takes log messages from a Logger and exports them. It might for example, write them to a console or write them to a file, or send them to a network logging service, or forward them to an OS log, or whatever. A Handler can be disabled by doing a setLevel(Level.


2 Answers

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.

like image 64
kamituel Avatar answered Oct 30 '22 03:10

kamituel


To answer you specific question:

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.

like image 30
pareshgoel Avatar answered Oct 30 '22 02:10

pareshgoel