博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ThreadLocal及其原理
阅读量:4125 次
发布时间:2019-05-25

本文共 5978 字,大约阅读时间需要 19 分钟。

ThreadLocal概念

ThreadLocal是一个本地线程副本变量工具类。主要用于将私有线程和该线程存放的副本对象做一个映射,各个线程之间的变量互不干扰,在高并发场景下,可以实现无状态的调用,特别适用于各个线程依赖不通的变量值完成操作的场景

下面通过一个例子来证明通过ThreadLocal能达到在每个线程中创建变量副本的效果:

public class MiloTheadLocal {
ThreadLocal
longLocal = new ThreadLocal
(); ThreadLocal
stringLocal = new ThreadLocal
(); public void set() {
longLocal.set(Thread.currentThread().getId()); stringLocal.set(Thread.currentThread().getName()); } public long getLong() {
return longLocal.get(); } public String getString() {
return stringLocal.get(); } public static void main(String[] args) throws InterruptedException {
final MiloTheadLocal test = new MiloTheadLocal(); test.set(); System.out.println(test.getLong()); System.out.println(test.getString()); Thread thread=new Thread() {
public void run() {
test.set(); System.out.println(test.getLong()); System.out.println(test.getString()); } }; thread.start(); //thread.join():用来指定当前主线程等待其他线程执行完毕后,再来继续执行Thread.join()后面的代码 thread.join(); System.out.println(test.getLong()); System.out.println(test.getString()); } }

【执行结果】:

1main10Thread-01main

从这段代码的输出结果可以看出,在main线程中和thread1线程中,longLocal保存的副本值和stringLocal保存的副本值都不一样。最后一次在main线程再次打印副本值是为了证明在main线程中和thread1线程中的副本值确实是不同的

ThreadLocal原理

ThreadLocal的内部结构图:

在这里插入图片描述

  • 每个Thread线程内部都有一个Map。
  • Map里面存储线程本地对象(key)和线程的变量副本(value)
  • Thread内部的Map是由ThreadLocal维护的,由ThreadLocal负责向map获取和设置线程的变量值。
public class Thread implements Runnable {
/* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLocals = null;}

get()方法:

/** * Returns the value in the current thread's copy of this * thread-local variable.  If the variable has no value for the * current thread, it is first initialized to the value returned * by an invocation of the {@link #initialValue} method. * * @return the current thread's value of this thread-local */public T get() {
Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) return (T)e.value; } return setInitialValue();}ThreadLocalMap getMap(Thread t) {
return t.threadLocals;}private T setInitialValue() {
T value = initialValue(); Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); return value;}protected T initialValue() {
return null;}

set()方法:

/** * Sets the current thread's copy of this thread-local variable * to the specified value.  Most subclasses will have no need to * override this method, relying solely on the {@link #initialValue} * method to set the values of thread-locals. * * @param value the value to be stored in the current thread's copy of *        this thread-local. */public void set(T value) {
Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value);}ThreadLocalMap getMap(Thread t) {
return t.threadLocals;}void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);}

remove()方法

/** * Removes the current thread's value for this thread-local * variable.  If this thread-local variable is subsequently * {@linkplain #get read} by the current thread, its value will be * reinitialized by invoking its {@link #initialValue} method, * unless its value is {@linkplain #set set} by the current thread * in the interim.  This may result in multiple invocations of the * initialValue method in the current thread. * * @since 1.5 */public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread()); if (m != null) m.remove(this);}ThreadLocalMap getMap(Thread t) {
return t.threadLocals;}

在ThreadLocalMap中,是用Entry来保存K-V结构数据的。但是Entry中key只能是ThreadLocal对象,这点被Entry的构造方法已经限定死了

static class Entry extends WeakReference
{
/** The value associated with this ThreadLocal. */ Object value; Entry(ThreadLocal k, Object v) {
super(k); value = v; }}

Entry继承自WeakReference(弱引用,生命周期只能存活到下次GC前),但只有Key是弱引用类型的,Value并非弱引用

在这里插入图片描述
每个线程只存一个变量,这样的话所有的线程存放到map中的Key都是相同的ThreadLocal,如果一个线程要保存多个变量,就需要创建多个ThreadLocal

ThreadLocalMap的问题

由于ThreadLocalMapkey是弱引用,而Value是强引用。这就导致了一个问题,ThreadLocal在没有外部对象强引用时,发生GC时弱引用Key会被回收,而Value不会回收,如果创建ThreadLocal的线程一直持续运行,那么这个Entry对象中的value就有可能一直得不到回收,发生内存泄露

【如何避免泄漏?】

既然Key是弱引用,那么我们要做的事,就是在调用ThreadLocal的get()、set()方法时完成后再调用remove方法,将Entry节点和Map的引用关系移除,这样整个Entry对象在GC Roots分析后就变成不可达了,下次GC的时候就可以被回收

在ThreadLocal的get,set的时候都会清除线程Map里所有key为null的value,但使用ThreadLocal的set方法之后,还是最好显示的调用remove方法

ThreadLocal
threadLocal = new ThreadLocal
();try {
threadLocal.set(new Session(1, "Misout的博客")); // 其它业务逻辑} finally {
threadLocal.remove();}

ThreadLocal的应用场景

【数据库连接】

private static ThreadLocal
connectionHolder = new ThreadLocal
() {
public Connection initialValue() {
return DriverManager.getConnection(DB_URL); } }; public static Connection getConnection() {
return connectionHolder.get(); }

【 Session管理】

private static final ThreadLocal threadSession = new ThreadLocal();    public static Session getSession() throws InfrastructureException {
Session s = (Session) threadSession.get(); try {
if (s == null) {
s = getSessionFactory().openSession(); threadSession.set(s); } } catch (HibernateException ex) {
throw new InfrastructureException(ex); } return s; }

转载地址:http://ofhpi.baihongyu.com/

你可能感兴趣的文章
【web素材】02-10款大气的购物商城网站模板
查看>>
6种方式实现JavaScript数组扁平化(flat)方法的总结
查看>>
如何实现a===1 && a===2 && a===3返回true?
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
12 个JavaScript 特性技巧你可能从未使用过
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>
8种ES6中扩展运算符的用法
查看>>
【视频教程】Javascript ES6 教程28—ES6 Promise 实例应用
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(下)
查看>>
【web素材】03-24款后台管理系统网站模板
查看>>
Flex 布局教程:语法篇
查看>>
年薪50万+的90后程序员都经历了什么?
查看>>
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>