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?
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).
So for example show a toast or a update a progress bar via a message (Runnable) posted to a handler but you can't if you start this runnable as a thread.
With handler you can also have things like MessageQueuing, scheduling and repeating.
I am yet to encounter a situation where I needed a thread in android.
I mostly use a combination of AsyncTasks and Handlers.
Handlers for the aforementioned tasks.
AsyncTasks for download/ data fetching and polling etc.
Simply says,
Both are same concepts, but some key differences. Thread is a java(java.lang) concept and Handler is a android(android.os)OS concept.if you are using Handler instead of Thread , your OS will automatically manage the working of background processes. But if you are using Thread,it is not dependent on your android OS. So Threads can't manage memory efficiently as compared to Handler.
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)
(orMessage
), you added theRunnable
object toLooper
and execute the code later in the same thread.A
Thread
, generallyMainThread
orUIThread
contains aLooper
. WhenMainThread
runs, it will loop theLooper
and executeRunnable
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.htmlWhen 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 useHandler
because it's lighter and faster.Please also refer to Handler vs AsyncTask vs Thread
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 haveLooper
attached by default, so it is common practice to update UI withHandler.post(Runable)
which means execute some piece of code on thread which is associated with thisHandler
.As soon as
Handler
servesLooper
, it can't be created in a thread which have no associatedLooper
.