The principle of inter-thread communication between Android

Source: Internet
Author: User

To understand the principle, read the fucking source

1, starting from Handlerthread.

Handlerthread is a class provided by the Android system, inheriting thread, which is a thread. Take a look at the Run method.

     public void Run () {        Mtid = Process.mytid ();        Looper.prepare ();//#1        synchronized (this) {            mlooper = Looper.mylooper ();//#2            notifyall ();        }        Process.setthreadpriority (mpriority);        Onlooperprepared ();        Looper.loop ();//#3        Mtid =-1;    }


in the 1th indicator of the code snippet, the prepare method that called Looper is invoked.

     private static void Prepare (Boolean quitallowed) {        if (sthreadlocal.get () = null) {            throw new RuntimeException (" Only one Looper could be created per thread ");        Sthreadlocal.set (New Looper (quitallowed));//#4    }

In the 4th indicator of the code snippet, the Looper instance is stored in the thread local variable threadlocal, and the Looper is bound to the current one.

In the 2nd indicator of the code snippet , the Looper instance that is bound to the current thread is obtained, and the current thread has the Looper instance.

The principle of inter-thread communication between Android

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.