The myth of the Android Open thread

Source: Internet
Author: User

Found some people who just learned Android, and I was the same, on the Android thread will have a certain misunderstanding.
In Android, when you start a new thread, some people use the following methods:

New Handler (). Post (R);

But this does not open a new thread, but simply adds the current task to the main thread. If you want to open a new thread, you should use Handlerthread, using the following:

New Handlerthread (name); Thread.Start (); New Handler (Thread.getlooper ()). Post (R);

If you use more of this, you can encapsulate a static method that gets an object that performs tasks on a new thread:

    /* *     * Gets the handler object that runs on the new thread.     *      * @param name     *            thread names.     * @return The handler object that is running on the new thread.      */     Public Static Handler getnewhandlerinotherthread (String name) {        new  handlerthread (name);        Thread.Start ();         return New Handler (Thread.getlooper ());    }

The myth of the Android Open thread

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.