多线程间共享变量线程安全问题——ThreadLocal

Java并发编程中很重要的类:ThreadLocal 在多线程应用程序中,对共享变量进行读写的场景是很常见的。如果不使用一定的技术或方案,会引发各种线程安全的问题。常见解决线程安全的方式有synchronized、volatile等方式,但synchronized对性能的开销大,volatile不能保证原子性,所以这里介绍一个 解决多线程间共享变量的线程安全问题 的方法——ThreadLocal

1.什么是 ThreadLocal


ThreadLocal 的作用,可以实现在同一个线程数据共享, 从而解决多线程数据安全问题.
ThreadLocal 可以给当前线程关联一个数据(普通变量、 对象、 数组)set 方法 [源码!]
ThreadLocal 可以像 Map 一样存取数据, key 为当前线程, get 方法
每一个 ThreadLocal 对象,只能为当前线程关联一个数据,如果要为当前线程关联多个数 据, 就需要使用多个 ThreadLocal对象实例
每个 ThreadLocal 对象实例定义的时候, 一般为 static 类型
ThreadLocal 中保存数据, 在线程销毁后, 会自动释放

threadlocal时序图:

image-20220403160618777

2、ThreadLocal的作用

多线程访问同一个共享变量时特别容易出现并发问题,特别是在多个线程需要对共享变量进行写入时。为了保证线程安全,一般使用者在访问共享变量时需要进行适当的同步,如图 1-3 所示

同步的措施一般是加锁,但加锁会在一定程度上增加系统的复杂度以及影响系统的性能。

为了解决多线程间共享变量的线程安全,ThreadLocal应运而生。

当创建一个ThreadLocal变量时,访问这个变量的每个线程都有这个变量的一个本地副本,当多个线程操作这个变量时,实际上就是操作自己本地内存里面的变量,从而避免了线程安全问题。图 1-3 就变成了 图1-4 如图:

3,、Threadlocal的使用示例 


public class ThreadLocalTest {
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
 
    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            threadLocal.set("threadLocal变量1");
            print("thread1");
            System.out.println("线程1的threadLocal变量的值为:"+threadLocal.get());
        });
 
        Thread thread2 = new Thread(() -> {
            threadLocal.set("threadLocal变量2");
            print("thread2");
            System.out.println("线程2的threadLocal变量的值为:"+threadLocal.get());
        });
 
        thread1.start();
        thread2.start();
    }
 
    public static void print(String s){
        System.out.println(s+":"+threadLocal.get());
     
    }
}

上述代码中,有一个 threadLocal 变量,类型为ThreadLocal ,然后创建了 thread1 和 thread2 ,并分别在两个线程中调用了 threadLocal.set(String str) 方法,然后用 threadLocal.get() 方法去获取threadLocal变量的值。显然,由输出结果可以知道,线程 thread1 中获取到的值就是它给threadLocal设置的值,即为本地变量1;线程 thread2 中获取到的值就是它给threadLocal设置的值,即为本地变量2。这两个线程是访问不到另外一个线程中的threadLocal的值的。

应用讲完了,现在着重来看一下ThreadLocal的实现原理(大厂面试必问~)

1、ThreadLocal 的 set、get方法

首先看下ThreadLocal 相关类的类图结构:

再看一下Thraed里面的成员变量

我们可以发现Thread类中有两个类型为ThreadLocalMap的变量,ThreadLoaclMap是一个定制化的HashMap。

在默认情况下,每个线程中的这两个变量都为null:

ThreadLocal.ThreadLocalMap threadLocals = null;
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;

只有当线程第一次调用ThreadLocal的set方法或get方法时才会创建它们。

public void set(T value) {
    //(1)获取当前线程
    Thread t = Thread.currentThread();
    //(2)将当前线程作为key,去查找对应的线程变量,找到则设置。
    ThreadLocalMap map = getMap(t);
    if (map != null) {
        map.set(this, value);
    }else {
        //(3)第一次调用set方法时,就创建当前线程对应的HashMap。
        createMap(t, value);
    }

}

(1)处代码首先获取调用set方法的线程,然后使用当前线程作为参数调用getMap(t) 方法,getMap(Thread t) 方法如下:

ThreadLocalMap getMap(Thread t) {

    return t.threadLocals;

}

可以看到,getMap(t) 的作用是获取线程自己的变量 threadLocals ,其类型是ThreadLocalMap

如果getMap(t)的返回值非空,则把value值存放到threadLocals中,即把当前变量值存放入当前线程的成员变量threadLocals中。

threadLocals是一个HashMap结构,其中key就是当前ThreadLocal的实例对象引用,value是通过set方法传递的值。

void createMap(Thread t, T firstValue) {
    t.threadLocals = new ThreadLocalMap(this, firstValue);

}

如果getMap(t)返回的是null,则说明是第一次调 set 方法,这时创建 当前线程的threadLocals 变量。 下面来看 createMap(t, value) 干了啥:

void createMap(Thread t, T firstValue) {

    t.threadLocals = new ThreadLocalMap(this, firstValue);

}

即创建了一个ThreadLocalMap对象,并将当前线程的threadLocals引用执行它。

再来看看get()方法的代码实现

public T get() {
		//(4)获取当前线程
        Thread t = Thread.currentThread();
        //(5)获取当前线程的threadLocals变量
        ThreadLocalMap map = getMap(t);
        //(6)如果threadLocals不为null,则返回对应的本地变量的值
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        //(7)threadLocals为空时,则初始化当前线程的threadLocals成员变量
        return setInitialValue();
    }
(4)处的代码首先获取当前线程实例,如果当前线程的threadLocals不为null,则直接返回当前线程绑定的本地变量;否则执行(7)处代码进行初始化。setInitialValue() 方法如下:
private T setInitialValue() {
		//(8)初始化为null
        T value = initialValue();
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        //(9)如果当前线程的threadLocals变量不为空
        if (map != null)
            map.set(this, value);
        else
        //(10)为空则创建一个ThreadLocalMap对象,并将当前线程的threadLocals引用执行它。
            createMap(t, value);
        return value;
    }

如果当前线程的threadLocals变量不为空,则设置当前线程的本地变量值为null;否则调用createMap方法创建ThreadLocalMap对象,并将当前线程的threadLocals引用执行它。

总结下:在每个线程里都有 threadLocals 的成员变量,该变量的类型为ThreadLocalMap(实际上可以理解为定制的HashMap),其中key为我们所定义的ThreadLocal变量的this引用,value则为set方法传递的值。每个线程的本地变量存放在线程自己的成员变量threadLocals中,如果当前线程一直不消亡,那么这些本地变量会一直存在,故可能会造成内存溢出,故使用完毕后需要使用 remove() 方法删除threadLocals中的本地变量

2、Threadlocal 不支持继承性

首先看下下面代码:

public class TestThreadLocal {

    //(1)创建线程变量
    public static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        //(2)赋值本地变量
        threadLocal.set("hello world");

        //(3)启动子线程
        new Thread(() -> {
            //(4)子线程输出线程变量的值
            System.out.println("子线程thread:" + threadLocal.get());
        }).start();


        //(5)主线程输出线程变量的值
        System.out.println("main线程:" + threadLocal.get());

    }
}

 

 输出结果说明:同一个 ThreadLocal 变量在父线程中被设置值后,在子线程中是获取不到的。
原因是:子线程里面调用get方法时,Thread t = Thread.currentThread() 代码是获取当前线程,当前线程是子线程,而调用set方法给threadLocal赋值的线程是main,两者是不同的线程,故子线程调用get方法取得的threadLocal值为null,main线程调用get方法取得的threadLocal值为“hello world”。

有没有方法让子线程能够访问到父线程中的值?继续往下看啦。

3、lnheritableThreadLocal 类

为了解决让子线程能够访问到父线程中的值的问题,lnheritableThreadLocal 应运而生。lnheritableThreadLocal 继承自 ThreadLocal,并提供了一个新特性:让子线程可以访问在父线程中设置的本地变量值。先来看下lnheritableThreadLocal 的实现:

public class InheritableThreadLocal<T> extends ThreadLocal<T> {
  	//(1)
    protected T childValue(T parentValue) {
        return parentValue;
    }
	//(2)
    ThreadLocalMap getMap(Thread t) {
       return t.inheritableThreadLocals;
    }
	//(3)
    void createMap(Thread t, T firstValue) {
        t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
    }
}

通过查看 InheritableThreadLocal 的源码可知,lnheritableThreadLocal 继承了 ThreadLocal 类并重新了 childValue、getMap、createMap方法。

由(3)处代码可知,InheritableThreadLocal 重写了 createMap 方法,那么当第一次调用 InheritableThreadLocal 实例的set方法时,创建的就是当前线程的inheritableThreadLocals变量的实例而不再是threadLocals了。

由(2)处代码可知,InheritableThreadLocal 重写了 getMap 方法,那么调用InheritableThreadLocal 实例的get方法时,就是获取当前线程的inheritableThreadLocals变量的实例而不再是threadLocals。

那么(1)处代码是如何实现子线程可以访问在父线程中设置的本地变量值的?

这要从创建Thread的代码将起,打开Thread类的默认构造函数:

public Thread(Runnable target) {
        init(null, target, "Thread-" + nextThreadNum(), 0);
 }
private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize) {
     init(g, target, name, stackSize, null, true);
}
   
private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals) {
        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        this.name = name;
        //(4)获取当前线程
        Thread parent = currentThread();
        SecurityManager security = System.getSecurityManager();
        if (g == null) {
            /* Determine if it's an applet or not */

            /* If there is a security manager, ask the security manager
               what to do. */
            if (security != null) {
                g = security.getThreadGroup();
            }

            /* If the security doesn't have a strong opinion of the matter
               use the parent thread group. */
            if (g == null) {
                g = parent.getThreadGroup();
            }
        }

        /* checkAccess regardless of whether or not threadgroup is
           explicitly passed in. */
        g.checkAccess();

        /*
         * Do we have the required permissions?
         */
        if (security != null) {
            if (isCCLOverridden(getClass())) {
                security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
            }
        }

        g.addUnstarted();

        this.group = g;
        this.daemon = parent.isDaemon();
        this.priority = parent.getPriority();
        if (security == null || isCCLOverridden(parent.getClass()))
            this.contextClassLoader = parent.getContextClassLoader();
        else
            this.contextClassLoader = parent.contextClassLoader;
        this.inheritedAccessControlContext =
                acc != null ? acc : AccessController.getContext();
        this.target = target;
        setPriority(priority);
        //(5)如果父线程的inheritableThreadLocals 变量不为null
        if (inheritThreadLocals && parent.inheritableThreadLocals != null)
         //(6)设置子线程中的 inheritableThreadLocals 变量
            this.inheritableThreadLocals =
                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
        /* Stash the specified stack size in case the VM cares */
        this.stackSize = stackSize;

        /* Set thread ID */
        tid = nextThreadID();
 }

由(4)处代码,获取了当前线程(main函数所在的线程,即父线程)

这里可能有同学会有疑问,这里获取到的当前线程为何是父线程?
想一下,当我们new Thread()的时候,是不是在main()方法里执行的,所以当前执行创建Thread代码的线程是main线程,所以(4)处代码中currentThread()方法获取到的就是父线程啦!

由(5)处代码,判断main线程里的inheritableThreadLocals 是否为null,不为null时,则执行代码(6)。

由(6)处代码,我们来看看createInheritedMap()方法:

static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
        return new ThreadLocalMap(parentMap);
    }

createInheritedMap方法中,使用父线程的inheritableThreadLocals变量作为构造函数创建了一个新的ThreadLocalMap对象,由(6)处:
this.inheritableThreadLocals=ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
知道将子线程的inheritableThreadLocals引用指向了这个新创建的ThreadLocalMap对象。

再看看 ThreadLocalMap(parentMap)构造函数:

private ThreadLocalMap(ThreadLocalMap parentMap) {
            Entry[] parentTable = parentMap.table;
            int len = parentTable.length;
            setThreshold(len);
            table = new Entry[len];
 
            for (int j = 0; j < len; j++) {
                Entry e = parentTable[j];
                if (e != null) {
                    @SuppressWarnings("unchecked")
                    ThreadLocal<Object> key = (ThreadLocal<Object>) e.get();
                    if (key != null) {
                    	//(7)调用了InheritableThreadLocal类重写的 childValue 方法
                        Object value = key.childValue(e.value);
                        Entry c = new Entry(key, value);
                        int h = key.threadLocalHashCode & (len - 1);
                        while (table[h] != null)
                            h = nextIndex(h, len);
                        table[h] = c;
                        size++;
                    }
                }
            }
        }

在构造函数中就是把父线程的inheritableThreadLocal变量的值复制到新的ThreadLocalMap对象中,(7)处代码实际上是调用了(1)处代码。

总结一下:InheritableThreadLocal实现子线程可以访问父线程的线程变量的实现原理如下:

  • InheritableThreadLocal通过重写createMap 和 getMap 方法让本地变量保存到了具体线程的inheritableThreadLocal变量中
  • 线程通过调用inheritableThreadLocal实例的setget方法时,就会创建当前线程的inheritableThreadLocal变量
  • 当父线程创建子线程时,构造函数会把父线程中的inheritableThreadLocal变量里面的本地变量值复制一份保存到子线程的inheritableThreadLocal变量里

将最开始的代码作以下修改:

public class TestThreadLocal {

    //(1)创建线程变量
    //public static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    //(1)创建线程变量
    public static InheritableThreadLocal<String> threadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) {
        //(2)赋值本地变量
        threadLocal.set("hello world");

        //(3)启动子线程
        new Thread(() -> {
            //(4)子线程输出线程变量的值
            System.out.println("子线程thread:" + threadLocal.get());
        }).start();


        //(5)主线程输出线程变量的值
        System.out.println("main线程:" + threadLocal.get());

    }

}

很多子线程需要使用父线程中的变量值的场景都可以使用InheritableThreadLocal,是不是很强大呢?

ThreadLocal、InheritableThreadLocal在Java并发编程中的地位举足轻重,理解了它们的底层实现和应用场景,会让你的大厂面试更有加分项

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值