Day Vision

Reading Everyday,Extending Vision

I bet you don't know the series: you haven't used ThreadLocal

2023-02-28 04:33:14


set接下来看一张图:image.png结合ThreadLocal的set方法来看一下: public void set(T value) { Thread t = Thread.currentThread();


image.png

Introduction

Let's throw a few small questions first.。

  1. As the question suggests, there really isn't a Java programmer who hasn't used the ThreadLocal class, right?
  2. What are the reference types in Java?
  3. ThreadLocal应用场景都有哪些?
  4. 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.

ThreadLocal

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:

set

接下来看一张图:

image.png

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.

public 

Notice how the Entry here turns out to be

这个弱引用引用的是ThreadLocal对象内存空间,而我们在新建ThreadLocal对象的时候一般是new出来的,

ThreadLocal

There is also a variable tl strongly referencing this ThreadLocal object.

Memory leak issues

内存泄露 Memory Leak:该回收的垃圾对象没有被回收,发生了内存泄露,垃圾对象越堆越多,可用内存越来越少,若可用内存无法存放新的垃圾对象,就会导致内存溢出。

当外部的强引用消失,如tl=null,那这个对象也就没啥意义了,现在只有我们的弱引用引用着这个对象内存空间了,它自然阻止不了垃圾回收掉这片空间。

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

get

get方法就是从先获取到当前Thread,然后拿到自身属性的map对象,根据ThreadLocal这个key去查看有没有对应Entry,有就获取value返回,没有则初始化一个value为null的Entry放入map中,返回null。

Application scenarios

首先要明白使用ThreadLocal是以耗费内存为代价的。

  1. 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.

  2. Dynamic switching of multiple data sources