In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the implementation principle of asynchronous data transfer of InheritableThreadLocal in java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the implementation principle of asynchronous data transfer of InheritableThreadLocal in java".
In Java, a Java thread is an operating system thread. Creating a thread needs to be created through new Thread, and JVM binds operating system threads for Thread. Even if you use a thread pool, you need to create threads through new Thread.
The Thread class has two ThreadLocal fields:
Public class Thread implements Runnable {ThreadLocal.ThreadLocalMap threadLocals = null; ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;}
InheritableThreadLocal is a subclass of ThreadLocal and is essentially a ThreadLocal.
In the Thread class, both threadLocals and inheritableThreadLocals are private to the thread object and can only be written and obtained through the current thread object, but Thread passes the data written to the inheritableThreadLocals to the inheritableThreadLocals of the child thread.
When we write data to ThreadLocal or InheritableThreadLocal, the writing process is:
1. ThreadLocal or InheritableThreadLocal first call the Thread#currentThread static method to get the Thread object of the current thread
2. Get the threadLocals or inheritableThreadLocals of the Thread object
3. Write the data to the threadLocals or inheritableThreadLocals field of the current Thread object by using ThreadLocal or InheritableThreadLocal object as key.
Therefore, the threadLocals of Thread and the key of inheritableThreadLocals are ThreadLocal or InheritableThreadLocal instances, and value is the written data.
I've already covered threadLocals in detail in my previous article, "understanding ThreadLocal in reverse, maybe it's easier to understand it this way." this article focuses on how inheritableThreadLocals is passed to child threads.
By default, when we create a thread using new Thread (), we get the current thread through Thread#currentThread in the constructor of Thread, using the current thread as the parent of the newly created thread, so there is a parent-child thread relationship.
No matter which overloaded constructor is used to create the Thread, the init method is called in the constructor to complete the initialization of the assignment to the Thread field, and there is a code in the init method:
Private void init (ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc, boolean inheritThreadLocals) {. If (inheritThreadLocals & & parent.inheritableThreadLocals! = null) this.inheritableThreadLocals = ThreadLocal.createInheritedMap (parent.inheritableThreadLocals); }
In the init method, because the inheritThreadLocals parameter defaults to true, as long as the parent thread's inheritableThreadLocals field is not empty, copy a copy of the parent thread's inheritableThreadLocals to the currently created thread object, which transfers the data stored by the parent thread's inheritableThreadLocals to the child thread.
What we have to consider with InheritableThreadLocal: memory leaks.
ThreadLocal.ThreadLocalMap uses array storage elements, unlike HashMap, it solves hash conflicts through open addressing, there is no linked list, and the array can store elements indefinitely by dynamically expanding the array. The type of array element is Entry.
When we write a key-value to ThreadLocal.ThreadLocalMap, ThreadLocalMap wraps key and value as an Entry, calculates the index value from the hashcode value of key, and puts Entry into an array.
The source code of the ThreadLocal.ThreadLocalMap.Entry class is as follows:
Static class Entry extends WeakReference
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.