The basic use of hanlder can be found in the blog "Android handler use", if you want to understand handler, Looper, thread and other relationships and internal implementation principles can be see the blog "Deep source analysis of Android handler , Message,messagequeue,looper ".
The description of Handlerthread in the API in Android is:
Handy class for starting a new thread, which has a looper. The looper can then is used to create handler classes. Note that start () must still is called.
This means that the Handlerthread class makes it easy to create a new thread with Looper. The looper can be used to create Hanlder objects. It is important to note that the Start method must be called.
Throw away Hanlderthread, we don't use this class to see how handler, Thread, Looper.
we can get the Looper object associated with the current thread through the Looper.mylooper () method. When a new thread is created, the new thread is initially not associated with the Looper and the corresponding message queue MessageQueue, and the external representation is to call Looper.mylooper () in the new thread to return NULL. If we are not aware of this, then we will encounter problems when we use handler in a new thread.
Suppose that in order to use handler in a new thread, we might write the following code:
class TestThread extends Thread { public Handler mHandler; publicvoidrun() { new Handler() { publicvoidhandleMessage(Message msg) { // process incoming messages here } }; } }
However, when actually executing, you will find that when you run to Mhandler = new Handler (), an exception is thrown:
Can ' t create handler inside thread that have not called looper.prepare ()
Exceptions can be thrown, see the source code of the handler constructor.
The reason for throwing an exception is that we do not pass Looper in the constructor, so Hanlder uses the default Looper in the constructor, and the default looper is obtained by calling Looper.mylooper (). when we call Looper.prepare (), we associate the Looper with the current thread. Therefore, only after the Looper.prepare () method is called, Looper.mylooper () can get the Looper object. so here's a hint that we need to call the Looper.prepare () method first.
In order to be able to create a normal use of handler in a new thread, we changed the code to look like this:
class LooperThread extends Thread { public Handler mHandler; publicvoidrun() { Looper.prepare(); new Handler() { publicvoidhandleMessage(Message msg) { //此处处理消息 } }; Looper.loop(); }}
In the new thread's Run method, we first called the Looper.prepare () method, which associates the Looper object to the current thread, then executes new Handler () and calls the Looper.mylooper inside the Hanlder constructor. () Gets the Looper object that is associated with the current thread. After creating the Hanlder object, we need to call the Looper.loop () method to loop the message queue.
With the code above, we can create and use the handler object in a new thread, but the problem is that it feels very wordy and inconvenient to write every time. To make it easier for developers to create in new threads and use handler,android to provide Handlerthread, Handlerthread is inherited from the thread class.
The sample code for using Handlerthread is as follows:
new HandlerThread("TestHandlerThread"new Handler(handlerThread.getLooper()) { publicvoidhandleMessage(Message msg) { //此处处理消息 };};
After we have created the handlerthread, we need to call its Start method, and after calling the Start method, the Run method executes in the Hanlderthread thread.
The source code for the Run method of the Handlerthread class is as follows:
publicvoidrun() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1;}
As we can see, the Looper.prepare () method is called first in the Run method, then the Looper object associated with the thread is obtained through the Looper.mylooper () method, and finally the Looper.loop () method is called to loop the message queue. as can be seen, Handlerthread's Run method is mainly to give us above the normal situation in the new thread to create handler code to do some encapsulation. After the Handlerthread object is created and its Start method is called, the handlerthread thread is already associated with the Looper object (through the Looper.prepare () method Association). and the message queue inside the thread loops (via the Looper.loop () method). Finally, we only need to pass the Looper object associated with the Handlerthread thread to Handler's constructor by Handlerthread.getlooper () when creating the handler object. As the API at the beginning of this article explains to Handlerthread: The Handlerthread class makes it easy to create a new thread with Looper. The looper can be used to create Hanlder objects. It is important to note that the Start method must be called.
Handlerthread is convenient to use because the Handlerthread class has done some work with looper within the Run method (invoking the Looper.prepare () and Looper.loop () methods), This way, our developers do not need to deal with Looper when they use it, thus improving the convenience of development. Handlerthread is not very advanced, just to our common development process to do the encapsulation, so we do not have to handlerthread and to achieve it is also possible, specific use without handlerthread according to their preferences.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The use and principle analysis of Handlerthread in Android