Let's throw a few small questions first.。
- As the question suggests, there really isn't a Java programmer who hasn't used the ThreadLocal class, right?
- What are the reference types in Java?
- ThreadLocal produces a memory leak, do you understand?Yes? What is a memory leak?
Some time ago, I made a small thing for Spring's dynamic data source switching, which is to achieve dynamic switching according to the requests under different packages through AOP, and ThreadLocal is used in order to prevent data connection disorder when multiple threads request at the same time.
How to understand this... For example, thread A uses a data source, thread B uses B data source, and thread B enters at the same time may cause A to use B data source.
If you've read the spring transaction control source code, you know that the connection in spring is placed in ThreadLocal.
Threadlocal is generally called a thread-local variable, which means that a ThreadLocal variable is accessible only to the current thread.
Each thread can manipulate this local variable via set() and get() without conflicting with other threads' local variables.
To sum it up:
Take a look at it in conjunction with ThreadLocal's set method:
Earlier we said call
Ou roared, and found that this Map turned out to be a static inner class in the ThreadLocal class.
No one should be able to understand the Entry in Map....There is a property Entry table in this Map, which is used to store our key value pair.
Notice how the Entry here turns out to be
There is also a variable tl strongly referencing this ThreadLocal object.
Memory leak issues
内存泄露 Memory Leak：该回收的垃圾对象没有被回收，发生了内存泄露，垃圾对象越堆越多，可用内存越来越少，若可用内存无法存放新的垃圾对象，就会导致内存溢出。
And the value of our Entry is still a strong reference ah, I Have No Key, you give me to keep the value of what use? This can lead to memory leaks and overflow if the available memory can not hold the new garbage object.
So how to solve this problem, we only need to use ThreadLocal after using it
Override explicit passing of parameters in multi-layered nested methods
That is, information is passed in context, and all methods within the thread can get it, avoiding some parameter passing.
Dynamic switching of multiple data sources