Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

While I was working with android, and I need something to run in the background. I use Threads to run it. Usually I would write a class that extends Thread and implement the run method.

I have also saw some examples that implments runnable and pass into runnable into Threads.

However I am still confused. Can someone give me a clear explanation?

  1. What is the point of Runnable if one can write the background code in the Thread's run method?
  2. How is Handler used inside thread and why do we need to use it.
  3. Android has another thing call runOnUiThread, How do we use that? I know that it is used for updating the UI.
like image 463
Hong Wei Wang Avatar asked Jan 17 '14 19:01

Hong Wei Wang


People also ask

What is the difference between thread and handler?

A Thread , generally MainThread or UIThread contains a Looper . When MainThread runs, it will loop the Looper and execute Runnable one by one. When Handler is preferred: When you want to update UI objects (like TextView text) from other thread, it is necessary that UI objects could only be updated in UI Thread.

What is difference between thread and runnable?

Runnable is an interface which represents a task that could be executed by either a Thread or Executor or some similar means. On the other hand, Thread is a class which creates a new thread. Implementing the Runnable interface doesn't create a new thread.

What is the difference between handler vs AsyncTask vs thread?

Using Handlers you have the advantage of MessagingQueues , so if you want to schedule messages or update multiple UI elements or have repeating tasks. AsyncTask are similar, in fact, they make use of Handler , but doesn't run in the UI thread, so it's good for fetching data, for instance fetching web services.

What is Handler and runnable in Android?

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 a Looper .


3 Answers

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.
like image 126
William Morrison Avatar answered Sep 20 '22 06:09

William Morrison


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 objects associated 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.

like image 44
Prem Avatar answered Sep 21 '22 06:09

Prem


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.

like image 23
marcinj Avatar answered Sep 21 '22 06:09

marcinj