What is the different between Handler, Runnable, and Threads? What is the different between Handler, Runnable, and Threads? multithreading multithreading

What is the different between Handler, Runnable, and Threads?


Why use Runnable over Thread?

  • Runnable separates code that needs to run asynchronously, from how the code is run. This keeps your code flexible. For instance, asynchronous code in a runnable can run on a threadpool, or a dedicated thread.

    A Thread has state your runnable probably doesn't need access to. Having access to more state than necessary is poor design.

    Threads occupy a lot of memory. Creating a new thread for every small actions takes processing time to allocate and deallocate this memory.

What is runOnUiThread actually doing?

  • Android's runOnUiThread queues a Runnable to execute on the UI thread. This is important because you should never update UI from multiple threads. runOnUiThread uses a Handler.

    Be aware if the UI thread's queue is full, or the items needing execution are lengthy, it may be some time before your queued Runnable actually runs.

What is a Handler?

  • A handler allows you to post runnables to execute on a specific thread. Behind the scenes, runOnUi Thread queues your Runnable up with Android's Ui Handler so your runnable can execute safely on the UI thread.


1. Why runnable ?

Runnable is just an interface you need to instantiate a thread to contain it. Whereas thread already contains the ability to spawn a thread.If you extend Thread you can't extend anything else (Java doesn't support multiple inheritance). You can have multiple interfaces on a class, therefore you could have Runnable.

Also, When you extends Thread class, each of your thread creates unique object and associate with it. When you implements Runnable, it shares the same object to multiple threads.

2. Why to Use handler and what is it ?

Handler is written in Java (internally use a Thread), so everything you can do with Handler, you can achieve using a Thread too.

So why should you use Handler? Reason is as below

  • Handler allows you to send and process Message and Runnable objectsassociated with a thread's MessageQueue. To put it in simple terms,handler makes your job easy.

  • Android has two main rules for handling threads:

  • Do not block the UI thread

  • Do not access the Android UI toolkit from outside the UI thread

To bind by the 2 rules stated above, In android we have 3 built-in methods that can handle the situation when one of your Activity classes are run on or called from a different thread.

We can then schedule the UI updates to be run on the UI thread with these three methods below. The Activity or View then works as a handler (more on handlers below) and schedules your runnable to the UI thread:

  1. Activity.runOnUiThread(Runnable)
    1. View.post(Runnable)
    2. View.postDelayed(Runnable, long) //(long = time to scheduling)

3. What is UI Thread ?

UI thread is the main thread in which the UI elements like View and Activity are rendered. Any time consuming operations should not happen in the UI Thread. The application default runs in UI Thread. You need not do anything special to use the UI Thread.


Handler, Runnable, and Threads actually work together, I dont think you should compare them.

Handler

allows send messages between two threads in a safe manner, that means that sending thread puts message into destination thread queue, and this destination queue will process this message in its appropriate time.

Runnable

this is an interface that you implement, in implementation you put logic you want to execute on some thread. You can actually use Runnable also in non thread related places. Lots of Java apis actually use Runnable, not only Thread's. You can post Runnable using handler, or you can use it with executors. Runnables are nice because you can implement them in a form of anonymous implementation.

UniThread

you meant UI Thread? Most user interfaces implements its workings in single thread, all UI elements: windows/widgets communicate using messages (just like in Handler). Ie. user presses button, this initiates a message with information that button was pressed, it is send to UI thread and finally delivered to your listener.

In Android it is forbidden (results in exception) to modify UI elements from non UI thread, this makes sense - if you would modify it from other thread this could happen while UI thread is doing some changes to the same widget - resulting in Undefined Behaviour.