First, how to download Android source code
In SDK Manager, select the sources for Android SDK.
Ii. Preliminary introduction of Threadlocal
1) Executes the set (Object obj) method of the ThreadLocal object (static final ThreadLocal sthreadlocal) and stores a key-value pair with the current thread as the key.
2) Execute the Get () method of the Threadlocal object, and the corresponding value will be fetched according to the current thread object as the key.
In the Looper method call that was mentioned earlier:
When Looper.prepare is executed, the system generates a Looper object, which is then stored (set) with the current thread through threadlocal. When the handler object is generated, the Looper object is taken (get) according to the name of the thread and assigned to the Mlooper member variable of the handler object. So the relationship between Looper and handler is connected by Threadlocal .
Generating a Looper object generates a MessageQueue object corresponding to it. After the handler object is generated, both the Looper object and the MessageQueue object are assigned to the member variables of the looper.
Android Learning Note (14) Handler theory supplement