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