相信很多人知道ThreadLocal是针对每个线程的,但是其中的原理相信大家不是很清楚,那咱们就一块看一下源码. 首先,我们先看看它的set方法.非常简单,从当前Thread中获取map.那么这个getMap方法是什么样的呢?咱们继续看. /** * Sets the current thread's copy of this thread-local variable * to the specified value. Most subclasses will have no need to…
ThreadLocal原理简单刨析 ThreadLocal实现了各个线程的数据隔离,要知道数据是如何隔离的,就要从源代码分析. ThreadLocal原理 需要提前说明的是:ThreadLocal只是一个向线程对象中存取数据的工具,ThreadLocal对象本身并不储存数据. 源码剖析 public class TestThread { public static void main(String[] args) { ThreadLocal<String> local = new Thread…