Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The reason why ThreadLocal variables can only be used for thread private

Shulou Source: shulou.com Published: 2022-06-02 07:51:31 10月02日 Update

This article mainly introduces "the reason why ThreadLocal variables can only be used for thread private". In daily operation, I believe many people have doubts about the reason why ThreadLocal variables can only be used for thread private. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt that "ThreadLocal variables can only be used for thread private". Next, please follow the editor to study!

The use of ThreadLocal usually starts with its set/get method.

The source code of the set method is as follows:

Public void set (T value) {Thread t = Thread.currentThread (); / / get the current thread object ThreadLocalMap map = getMap (t); / / get the thread's ThreadLocalMap object if (map! = null) map.set (this, value) from the current thread object; / / put the ThreadLocal object as key,value in ThreadLocalMap else createMap (t, value);}

The source code of the get method is as follows:

Public T get () {Thread t = Thread.currentThread (); / / get the current thread object ThreadLocalMap map = getMap (t); / / get the current thread's ThreadLocalMap object if (map! = null) {ThreadLocalMap.Entry e = map.getEntry (this) / / get its value if (e! = null) {@ SuppressWarnings ("unchecked") T result = (T) e.value from ThreadLocalMap using the ThreadLocal object as its key; return result;}} return setInitialValue ();}

As can be seen from the above code, when ThreadLocal sets the value, it first gets the thread object that currently operates the ThreadLocal variable, and then stores the ThreadLocal object and value in the ThreadLocalMap of the current thread object in the form of key-value pairs, so that each thread will assign a different value to the variable according to its own situation and exist in the current thread object when executing the code.

In the get value, the current operation thread object is also obtained first, and then the corresponding value of the ThreadLocal object is obtained from the ThreadLocalMap of the current thread object.

So variables set through ThreadLocal have their own copy within each thread and are therefore thread private.

At this point, the study on "why ThreadLocal variables can only be used for thread private" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Threads objects variables private causes methods learning code more source code help different practical and then copy that is form situation article theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi vpn MySQL Xiaomi