The use of Android drip Handlerthread

Source: Internet
Author: User

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);
?
1
?
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.