In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the role of ThreadLocal in java". In daily operation, I believe that many people have doubts about the role of ThreadLocal in java. 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 question of "what is the role of ThreadLocal in java?" Next, please follow the editor to study!
Talk about the unusual but indispensable ThreadLocal
All variables in the memory model are stored in the main memory. When a variable is to be used in a thread, it needs to be copied from the main memory into its thread before it can be operated. In this case, the thread operates a copy of the main memory variable, and then flushes it back to the main memory after the operation is completed. The essence of brushing back is variable assignment.
If multiple threads access the same variable, each thread has a copy, and after the operation, it will be brushed back to the main memory, and the flushing time exists in order, then the assignment will be in order, and of course, the latter will overwrite the former, which is the secondary cause of visibility problems.
After introducing the above knowledge points, we will explain ThreadLocal. A thread wants to use a variable, so it copies the shared variable from main memory into the thread. The next time you want to use the variable after use, the copy of the variable is the last copy, not the copy copied again from the variable in main memory, and you don't want other threads to affect the variable. This is the purpose of ThreadLocal, which is not implemented by sharing variables. The details are described below.
The purpose is clear, but to follow the memory model specification in the JAVA memory model, let's take a look at how JDK meets both the memory model specification and the ThreadLocal purpose.
Meet the memory model
This is very simple, that is, what you should do, it is still under your jurisdiction, copy it when you want to copy it, brush it back if you want to brush it back, and it will still make you invisible.
Meet the purpose of ThreadLocal
A variable that can be accessed by multiple threads is called a shared variable. If you control how the variable is accessed, it is OK to make it inaccessible to other threads. The control method is to correspond the thread to the variable one by one, and control the access entry of the variable to only the thread. The practice in JDK is to let the thread hold the variable (bind to the thread itself).
The thread can bind variables, but doesn't know how many need to be bound, so the storage function is handed over to a special data structure-> Map. And specially designed a tool to access this Map, this tool is ThreadLocal. And the key of this Map is the reference address of the ThreadLocal instance, and value stores the real variables.
In this way, the design achieves its goal. When ThreadLocal builds, it receives a generic type that tells you that the stored variable is an object type.
ThreadLocal Design Core Map Design static class ThreadLocalMap {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.