Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is ThreadLocal in Inheritable

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is ThreadLocal in Inheritable". In daily operation, I believe many people have doubts about what is ThreadLocal in Inheritable. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "what is ThreadLocal in Inheritable?" Next, please follow the editor to study!

1. Introduction in the last ThreadLocal detailed explanation, we introduced the principle and design of ThreadLocal in detail, and analyzed ThreadLocal from the source code level. However, because ThreadLocal is designed to bind the current thread, it will be quite difficult to implement if you want the ThreadLocal of the current thread to be used by child threads (you need to pass it in the code yourself). Under this background, InheritableThreadLocal came into being.

Inheritable thread-local variables are used in preference to ordinary thread-local variables when the per-thread-attribute being maintained in the variable (e.g., User ID, Transaction ID) must be automatically transmitted to any child threads that are created.

2, application call chain tracking: in the call chain system design, in order to optimize the system running speed, will use multi-thread programming, in order to ensure that the call chain ID can naturally transfer between multiple threads, need to consider the ThreadLocal transfer problem (most systems will use thread pool technology, this is not only InheritableThreadLocal can solve, I will introduce the related technology implementation in another article).

3. The InheritableThreadLocal class InheritableThreadLocal rewrites three functions of ThreadLocal:

`/ * this function creates a child thread in the parent thread and copies the InheritableThreadLocal variable to the child thread using * / protected T childValue (T parentValue) {return parentValue;} / * because getMap is overridden, when operating InheritableThreadLocal, * it will only affect the inheritableThreadLocals variable in the Thread class, and * it is no longer related to the threadLocals variable * / ThreadLocalMap getMap (Thread t) {return t.inheritableThreadLocals } / * is similar to getMap. When operating InheritableThreadLocal, * will only affect the inheritableThreadLocals variable in the Thread class, and * it is no longer related to the threadLocals variable * / void createMap (Thread t, T firstValue) {t.inheritableThreadLocals = new ThreadLocalMap (this, firstValue);} `

Note: because the getMap () and createMap () functions are overridden, when

4. When it comes to InheritableThreadLocal, we should also start with the Thread class:

Public class Thread implements Runnable {. (other source code) / * * the ThreadLocalMap of the current thread, which mainly stores the thread's own ThreadLocal * / ThreadLocal.ThreadLocalMap threadLocals = null;/* * InheritableThreadLocal, the ThreadLocalMap integrated from the parent thread, * mainly used for the transfer of ThreadLocal variables between parent and child threads * this article mainly discusses this ThreadLocalMap * / ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;. (other source code)

} `

The Thread class contains two variables, threadLocals and inheritableThreadLocals, where inheritableThreadLocals is the main store of ThreadLocal.ThreadLocalMap that can be automatically passed to child threads. Next, let's take a look at the process of a parent thread creating a child thread. Let's start with the simplest way:

4.1.User creates Thread, Thread thread = new Thread ()

4.2. Thread creation

Public Thread () {init (null, null, "Thread-" + nextThreadNum (), 0);}

4.3.initialization of Thread

/ * by default, set inheritThreadLocals to pass * / private void init (ThreadGroup g, Runnable target, String name, long stackSize) {init (g, target, name, stackSize, null, true);} / * initialize a thread. * there are two calls to this function, * 1. Init () above. Do not pass AccessControlContext,inheritThreadLocals=true * 2, pass AccessControlContext,inheritThreadLocals=false * / private void init (ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc, boolean inheritThreadLocals) {. (other code) if (inheritThreadLocals & & parent.inheritableThreadLocals! = null) this.inheritableThreadLocals = ThreadLocal.createInheritedMap (parent.inheritableThreadLocals) . (other codes)} `

As you can see, when the child thread is generated by default, inheritThreadLocals=true; passes the parent thread inheritableThreadLocals to the child thread if the parent thread inheritableThreadLocals is not empty at this time.

4.4. ThreadLocal.createInheritedMap Let's continue to track createInheritedMap:

`static ThreadLocalMap createInheritedMap (ThreadLocalMap parentMap) {return new ThreadLocalMap (parentMap);} / * build a ThreadLocalMap containing all Inheritable ThreadLocals in parentMap * this function is only called by createInheritedMap (). * / private ThreadLocalMap (ThreadLocalMap parentMap) {Entry [] parentTable = parentMap.table; int len = parentTable.length; setThreshold (len); / / ThreadLocalMap uses Entry [] table to store ThreadLocal table = new Entry [len]; / / copy parentMap records for (int j = 0; j < len; jacks +) {Entry e = parentTable [j] one by one If (e! = null) {@ SuppressWarnings ("unchecked") ThreadLocal key = (ThreadLocal) e.get () If (key! = null) {/ / some students may wonder why childValue is used here instead of assigning values directly. / / after all, childValue also returns e.value directly inside. / / personal understanding, mainly to reduce the difficulty of reading code Object value = key.childValue (e.value); Entry c = new Entry (key, value); int h = key.threadLocalHashCode & (len-1) While (table [h]! = null) h = nextIndex (h, len); table [h] = c; size++;}} `

From the ThreadLocalMap, the child thread copies all the records in the parentMap to its own thread one by one.

5. Summary InheritableThreadLocal is mainly used for child thread creation, it needs to automatically inherit the ThreadLocal variable of the parent thread to facilitate the further transmission of necessary information.

At this point, the study of "what is ThreadLocal in Inheritable" is over. I hope to be able to solve your 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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report