Two threads waiting for each other

Source: Internet
Author: User

This code is a phenomenon found when viewing the source code of Android Activitymanagerservice, recording

--Athread run ()--do Some thing---notify main Thread---wait

Mainthread-----notified and do some thing--notify Athread

    public static final Context main (int factorytest) {athread thr = new Athread ();        Thr.start ();                Synchronized (THR) {while (Thr.mservice = = null) {try {thr.wait ();        } catch (Interruptedexception e) {}}}//do some thing ...            Synchronized (THR) {Thr.mready = true;    Thr.notifyall ();//Notify Athread} return context;        }//Activitymanagerservice's internal class static classes Athread extends Thread {activitymanagerservice mservice;        Looper Mlooper;        Boolean mready = false;        Public Athread () {Super ("Activitymanager");            } @Override public void Run () {looper.prepare ();                ... synchronized (this) {mservice = m;                Mlooper = Looper.mylooper (); Watchdog.getinstance (). Addthread (New Handler (MloopER), getName ());                    Notifyall ();//notifies main thread} synchronized (this) {while (!mready) {                    try {wait ();        } catch (Interruptedexception e) {}}} looper.loop (); }    }


Two threads waiting for each other

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.