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