L

ooper

Looper is a message handling loop: it reads and processes items from a MessageQueue. The Looper class is usually used in conjunction with a HandlerThread (a subclass of Thread).

Where we can use Looper class?

If someone wants to execute multiple messages(Runnables) then he should use the Looper class which is responsible for creating a queue in the thread. For example, while writing an application that downloads files from the internet, we can use Looper class to put files to be downloaded in the queue.

class LooperThread extends Thread {
   public Handler mHandler;
   public void run() {
     Looper.prepare();

     mHandler = new Handler() {
       public void handleMessage(Message msg) {
         // process incoming messages here
       }
     };

     Looper.loop();
   }
 }

Hander

Handler is a utility class that facilitates interacting with a Looper—mainly by posting messages and Runnable objects to the thread’s MessageQueue. When a Handler is created, it is bound to a specific Looper (and associated thread and message queue).

(1) to schedule messages and runnables to be executed at some point in the future

(2) to enqueue an action to be performed on a different thread than your own.

Thread

Execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}

HandlerThread

Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

HandlerThread has their own looper and MessageQueue or simply queue, other than the Thread.

Thread cannot be reused after the job is completed, while thread with Looper (HandlerThread)is kept alive by Looper until you call quit method, so you don’t need to create a new instance each time you want to run a job in background.

HandlerThread handlerThread = new HandlerThread("MyHandlerThread");
handlerThread.start();
Looper looper = handlerThread.getLooper();
Handler handler = new Handler(looper);
handler.post(new Runnable(){…});