Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Thread and Handler

Can somebody tell me the deference between Thread and Handler? When we use Thread and when we use Handler?

I have two code in my project , But I can't understand them.

final Handler handler =  new Handler() {     @Override     public void handleMessage(Message msg)     {         //  Do SomeThings     }  }; 

And

private class readThread extends Thread {     Handler mHandler;      readThread(Handler h){         mHandler = h;         this.setPriority(Thread.MIN_PRIORITY);      }      @Override     public void run()     {         //  Do SomeThings     } } 

And in another method call the handler like this

read_thread = new readThread(handler);             read_thread.start(); 

Which one run first?? Can somebody explain me?

like image 758
Khodayar Avatar asked Aug 01 '15 11:08

Khodayar


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

How many handlers can a thread have?

There can be any number of handlers for one single thread. So, the looper is providing the thread with the facility to run in a loop with its own MessageQueue.

What is difference between thread and AsyncTask?

Thread can be triggered from any thread, main(UI) or background; but AsyncTask must be triggered from main thread. Also on lower API of Android(not sure, maybe API level < 11), one instance of AsyncTask can be executed only once. Long task in general.


2 Answers

The same: you can both execute task asynchronously without blocking your current code,

The difference: Imagine you have a Runnable r = new Runnable{...}

  • When you use new Thread(r).start(), you actually created a new thread and run task asynchronously.

  • When you use new Handler().post(r) (or Message), you added the Runnable object to Looper and execute the code later in the same thread.

A Thread, generally MainThread or UIThread contains a Looper. When MainThread runs, it will loop the Looper and execute Runnable one by one.

When Thread is preferred:

When you're doing a heavy work like network communication, or decoding large bitmap files, a new thread is preferred. If a lot of thread is needed, maybe ExecutorService is preferred further. https://developer.android.com/reference/java/util/concurrent/ExecutorService.html

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. Also, when you just want to run some light code later (like the delay for 300ms) you can use Handler because it's lighter and faster.

Please also refer to Handler vs AsyncTask vs Thread

like image 97
yjzhang Avatar answered Sep 21 '22 00:09

yjzhang


Thread actually creates new thread - part of job running in background relatively to current thread.

Handler itself doesn't provide any mechanisms for background job - it is just a tool to access message queue (Looper) associated with some thread. UI thread have Looper attached by default, so it is common practice to update UI with Handler.post(Runable) which means execute some piece of code on thread which is associated with this Handler.
As soon as Handler serves Looper, it can't be created in a thread which have no associated Looper.

like image 39
Viacheslav Avatar answered Sep 21 '22 00:09

Viacheslav