TLS (thread Local Storage) solves the mutex problem of multi-threaded access to critical resources by allocating more memory, that is, each thread has its own critical resource object, so there is no access violation, and no lock mechanism control is needed to compare the typical space-to-time strategy. The following is an example of Looper, which describes how TLS is implemented in Android. You can see several key points to the TLS technology: 1. A value (encapsulated hash table) is saved in each Thread object 2. The Threadlocal static object is the key, and its corresponding values are the critical resources used by the thread object. Such a critical object can be created by threadlocal a static object in each thread, or it can be obtained from the static object and will not be share. Take handler constructs as an example to analyze how critical resources are accessed through threadlocal in threads:
The implementation mechanism of threadlocal