Reprint Please specify source: http://blog.csdn.net/lskshz/article/details/25364909. Introduction
Handlerthread inherits from thread, and when the thread is turned on, that is, when it runs, the Run method
The thread creates a looper with a message queue at the same time, and provides its own get method for this Looper object, which is the only difference from the normal thread.
Two. Benefits
Why use Handlerthread.
1. If you use the new Thread () {...} more than once in development. Start ()
Opening a child thread in this way creates multiple anonymous threads, making the program more and more slow to run.
and Handlerthread bring looper so that he can use the message to repeatedly reuse the current thread, saving money;
The looper in the handler class provided by the 2.android system is bound by the message queue of the UI thread by default.
For non-UI threads and to use the message mechanism, the Handlerthread internal looper is the most appropriate, and it does not interfere with or block the UI thread.
Three. Usage Handlerthread since the essence is thread, why did you add a handler in front of you? Android in the handler class is essentially from its internal looper in the message, and then trigger its internal callback interface Handlemessage method, let the user to achieve the specific processing of the message.
and Handlerthread itself with Looper, as long as it implements the callback interface,
Then Handlerthread can also process messages from its own thread within its own thread,
Fully implement message processing under lower expenses in non-UI threads.
Four. Code examples?
| 12345 |
MyHandlerThread myHandler = newMyHandlerThread("lsk"); myHandler.setPriority(Thread.MIN_PRIORITY); myHandler.start(); Handler handler = newHandler(myHandler.getLooper(), myHandler); |
?
?
| 12345678 |
class MyHandlerThread extends HandlerThread implements Callback { public MyHandlerThread(String name) { super(name); } @Override public boolean handleMessage(Message msg) { } } |
The use of Android drip Handlerthread