The implementation mechanism of threadlocal

Source: Internet
Author: User

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

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.