ThreadLocal源码解析

一.ThreadLocal简介
ThreadLocal 提供了线程局部变量,提供的局部变量与其他的变量不同,每个线程都可以通过set与get方法来访问独立初始化变量副本。
ThreadLocal 可以理解为线程本地变量,如果定义了一个ThreadLocal对象, 每个线程中的ThreadLocal读写是线程隔离的,相互不影响,它提供了一种将可变数据通过每个线程有自己的独立副本从而实现线程封闭的机制

包路径:java.lang.ThreadLocal

image.png

二.ThreadLocal相关类

ThreadLocal中嵌套内部类ThreadLocalmap ,这个类本质上就是一个map,与HashMap之类的实现相似,依然是Key-Value的形式,其中一个内部类Entry ,其中key可以看做是ThreadLocal实例,但是其本质是持有ThreadLocal的实例弱引用对象

image.png

在ThreadLocalMap中并没有对于ThreadLocalMap的引用,而是ThreadLocalMap在Thread类中,每个线程都向ThreadLocal里面塞值的时候,其实都是向自己持有的ThreadLocalMap里面写入数据,读的时候同理,首先从自己线程中取出自己持有的ThreadLocalMap,然后再根据ThreadLocal引用作为key取出Value,ThreadLocal实现了变量的线程隔离

image.png

Thread 源码部分截图

UML类图

image.png
image.png

ThreadLocal原理图

image.png

首先主线程定义的两个ThreadLocal变量 ,两个子线程 线程A和线程B
线程A和线程B 分别持有一个ThreadLocalMap 用于保存自己的副本,主线程的ThreadLocal中封装了set和get方法
在线程A和线程B中调用ThreadLocal的Set方法,会首先通过getMap(Thread.currentThread)获取到线程A或者线程B持有的ThreadLocalMap对象,在调用map.put方法,并将ThreadLocal作为key,将要存储的数据作为value 来存放
get和set原理是类似,先获取当前调用线程的ThreadLocalMap,在从map中获取value ,并将ThreadLocal作为key

三.ThreadLocalMap源码分析
在分析ThreadLocalMap 的同时,结合ThreadLocal的方法一起进行分析

       private static final int INITIAL_CAPACITY = 16;
        private ThreadLocal.ThreadLocalMap.Entry[] table;
        private int size;
        private int threshold;

        private void setThreshold(int var1) {
            this.threshold = var1 * 2 / 3;
        }

        private static int nextIndex(int var0, int var1) {
            return var0 + 1 < var1 ? var0 + 1 : 0;
        }

        private static int prevIndex(int var0, int var1) {
            return var0 - 1 >= 0 ? var0 - 1 : var1 - 1;
        }

存储数据结构 ---Entry 类 继承WeakReference

        /**
         * The entries in this hash map extend WeakReference, using
         * its main ref field as the key (which is always a
         * ThreadLocal object).  Note that null keys (i.e. entry.get()
         * == null) mean that the key is no longer referenced, so the
         * entry can be expunged from table.  Such entries are referred to
         * as "stale entries" in the code that follows.
         */
        static class Entry extends WeakReference> {
            /** The value associated with this ThreadLocal. */
            Object value;

            Entry(ThreadLocal k, Object v) {
                super(k);
                value = v;
            }
        }

Entry 继承WeakReference 使用弱引用,可以将ThreadLocal对象的生命周期和线程生命周期解绑 ,持有对ThreadLocal的弱引用,可以使得ThreadLocal在没有其他强引用的时候被GC回收掉,这样可以避免因为线程得不到摧毁 导致ThreadLocal对象无法被垃圾回收器回收

参考文档

ThreadLocal源码分析

你可能感兴趣的:(ThreadLocal源码解析)