Thread ThreadLocal threadlocalmap Relationship __threadlocal

Source: Internet
Author: User
Tags thread class

1. There is a variable in the Thread class that is Threadlocalmap type

2. This threadlocalmap is responsible for saving all the ThreadLocal variables we create.

3. Then, we do not need to pay attention to how the ThreadLocal variable is associated with Thread, direct Get/set OK


Threadlocalmap is the container of many ThreadLocal variables that we create, and this container is a member of a thread that is monopolized


The Key in Threadlocalmap is the reference to the ThreadLocal variable we created.

So when calling Tl1.set or Tl1.get, you can find yourself in the MAP according to the TL1 reference, reading or updating your own values


And Threadlocalmap is a member variable of a thread, so get the current thread first when getting threadlocalmap,

Then read its THREADLOCALMAP member (variable name threadlocals)


The ThreadLocal type of variable, which is equivalent to supporting the operation of the Threadlocalmap internally, saves us from placing the variable in the map, directly to a set itself and putting itself into the map.


In fact, if Threadlocalmap open operation interface, we can define ordinary variable (not ThreadLocal type),

Manually put in the Threadlocalmap, the effect should be similar,

In short, the ThreadLocal type of variable is inherently supporting operations Threadlocalmap

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.