总述

    最近做了一个日志调用链路跟踪的项目,涉及到操作标识在线程和子线程,线程池以及远程调用之间的传递问题。最终采用了阿里开源的TransmittableThreadLocal插件(https://github.com/alibaba/transmittable-thread-local)完美解决。在分析源码以及中途修复bug的过程中,被ThreadLocal搞得晕头转向。好在静下心来细细啃了一下午,终于能理解各种ThreadLocal相关问题了。这里准备用博客记录下来。

关于弱引用

    要想了解ThreadLocal的底层原理首先就要了解弱引用。本篇不会详细介绍是强引用,啥是弱引用、软引用以及虚幻引用,有兴趣的同学可以自己百度。这里直接给出弱引用的简单代码说明:

Object obj = new Object();
WeakReference<Object> wf = new WeakReference<Object>(obj); // 对堆内存中对象建立一个弱引用
obj = null; // 去掉堆中对象的强引用
System.gc();
System.out.println(wf.get()); // 输出null

可以看到弱引用的作用就在于当堆内存中对象不存在强引用的时候,在下一次gc的时候可能会回收掉堆内存占用。

走进ThreadLocal

    了解了弱引用之后,其实就能够很好地理解ThreadLocal(题外话:其实这个ThreadLocal我摸索了好久才弄得比较透彻)。直接上代码:

    首先我们需要注意Thread类中的两个属性:

public class Thread implements Runnable {
// .... /* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
// ThreadLocal实际值的存储所在
ThreadLocal.ThreadLocalMap threadLocals = null; /*
* InheritableThreadLocal values pertaining to this thread. This map is
* maintained by the InheritableThreadLocal class.
*/
// 后面要将的InheritableThreadLocal的实际值存储所在
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
}

这两个属性特别关键:

  • 他是每个线程所特有的
  • 两个属性的类型是ThreadLocal的内部静态类

他们是ThreadLocal的神奇魔法之关键~

    接下来我们来看看ThreadLocal的关键方法:

/**
* 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);
} /**
* Create the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
*
* @param t the current thread
* @param firstValue value for the initial entry of the map
*/
void createMap(Thread t, T firstValue) {
// set时候的关键,实际上是创建一个当前ThreadLocal的弱引用为key的Map
t.threadLocals = new ThreadLocalMap(this, firstValue);
} /**
* 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) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
} /**
* Get the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
* 最为关键的方法: 可以看出getMap实际上就是得到传入线程的threadLocals属性的值
* @param t the current thread
* @return the map
*/
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
} /**
* 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
* {@code initialValue} method in the current thread.
*
* @since 1.5
*/
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}

不难看出,所有方法都是围绕着一个ThreadLocalMap来操作的,那么这个ThreadLocalMap究竟是啥,我们进一步来分析:

/**
* ThreadLocalMap is a customized hash map suitable only for
* maintaining thread local values. No operations are exported
* outside of the ThreadLocal class. The class is package private to
* allow declaration of fields in class Thread. To help deal with
* very large and long-lived usages, the hash table entries use
* WeakReferences for keys. However, since reference queues are not
* used, stale entries are guaranteed to be removed only when
* the table starts running out of space.
*/
static class ThreadLocalMap { /**
* 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.
*/
// 实际上他的存储也是利用Entry结构来进行的,只不过这个Entry的key值是弱音用对象,实际上可以将ThreadLocalMap看做WeakHashMap
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value; Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
}

至此,我们已经知道了ThreadLocal是如何实现的了,具体来说是下面几个关键点:

  • ThreadLocal本身并不存储值,而是作为ThreadLocalMap的key用来查找对象所存储的值的
  • 用来存储值的ThreadLocalMap是每个线程都有的非静态属性,当前线程实例该属性的值对其他线程实例是不可见的,这也就实现了线程隔离
  • ThreadLocal的get方法实际上是先获取当前线程的ThreadLocalMap属性值,然后再通过ThreadLocal作为key获取实际上存储在Map中的值
  • 因为ThreadLocalMap的Key是软引用的,所以如果ThreadLocal不存在强引用且线程被回收的话,存储在已回收线程ThreadLocalMap中的值也是会被回收的。这一点是通过两方面来实现的:1. Key是软引用,当没有强引用指向ThreadLocal时,ThreadLocalMap的以该ThreadLocal作为key的Entry中key会在gc时被回收置为null 2. 调用ThreadLocal的set/get/remove方法的时候会触发Entry的expungeStaleEntry方法,方法会将key为null的value值回收

ThreadLocal剧集(一)的更多相关文章

  1. 我已看过的TVB剧集目录(陆续更新)

    2016年度TVB剧集 <一屋老友记> 主演:欧阳震华,胡定欣,滕丽名,罗兰 <纯熟意外> 主演:吴启华,蔡思贝,李施嬅,黎诺懿 <廉政行动2016> 主演:陈展鹏 ...

  2. 分布式改造剧集2---DIY分布式锁

    前言: ​ 好了,终于又开始播放分布式改造剧集了.前面一集中(http://www.cnblogs.com/Kidezyq/p/8748961.html)我们DIY了一个Hessian转发实现,最后我 ...

  3. 历年至今TVB剧集目录(持续更新...我已看过的推荐)

    2019年度TVB剧集 <> 主演: <> 主演: 2018年度TVB剧集 <大帅哥> 主演:张卫健,蔡思贝,洪永城,曹永廉 <守护神之保险调查> 主演 ...

  4. Android TV开发总结(七)构建一个TV app中的剧集列表控件

    原文:Android TV开发总结(七)构建一个TV app中的剧集列表控件 版权声明:我已委托"维权骑士"(rightknights.com)为我的文章进行维权行动.转载务必转载 ...

  5. nodejs:使用puppeteer在服务器中构建一个获取电影电视剧剧集的接口

    首先我们看下数据来源: 来源于这个网站:https://z1.m1907.cn/ 可以说这个网站上能找到很多你想看的很多电影或电视剧,最重要的是很多电影电视剧在别的网站是收费的,但是在这里看是免费的, ...

  6. 分布式改造剧集之Redis缓存采坑记

    Redis缓存采坑记 ​ 前言 ​ 这个其实应该属于分布式改造剧集中的一集(第一集见前面博客:http://www.cnblogs.com/Kidezyq/p/8748961.html),本来按照顺序 ...

  7. 分布式改造剧集三:Ehcache分布式改造

    第三集:分布式Ehcache缓存改造 前言 ​ 好久没有写博客了,大有半途而废的趋势.忙不是借口,这个好习惯还是要继续坚持.前面我承诺的第一期的DIY分布式,是时候上终篇了---DIY分布式缓存. 探 ...

  8. 大型情感剧集Selenium:4_老中医教你(单/多/下拉框)选项定位 #华为云·寻找黑马程序员#

    今天讲什么 讲什么标题说了,讲selenium的单选.多选.下拉框选项定位.但其实这东西,没什么太多说的,又比较枯燥,那该怎么让这一集selenium的课程变得有趣呢?有请老中医,哈哈- 怎么样,这个 ...

  9. 大型情感剧集Selenium:2_options设置 #华为云·寻找黑马程序员#

    上集回顾 昨天说简单介绍了什么是selenium,它能干what,和发展史与梗概.当的是python如何通过pip安装selenium,并下载对应浏览器的webdriver. 最后简单通过一个Demo ...

随机推荐

  1. AngularJS controller as vm方式

    从AngularJS1.20开始引入了Controller as 新语法,以前版本在Controller 中必须注入$scope这个服务,才能在视图绑定中使用这些变量,$scope不是那么POJO(普 ...

  2. 关于国密算法 SM1,SM2,SM3,SM4 的笔记

    国密即国家密码局认定的国产密码算法.主要有SM1,SM2,SM3,SM4.密钥长度和分组长度均为128位. SM1 为对称加密.其加密强度与AES相当.该算法不公开,调用该算法时,需要通过加密芯片的接 ...

  3. http发送请求方式;分为post和get两种方式

    http发送请求方式:分为post和get两种方式

  4. Java学习--数组的定义和使用

    1. 数组分配了空间,未赋值 public class ArrayDemo01{ public static void main(String args[]){ int score[] = null ...

  5. Android-AndroidStudio-AVD启动不了-emulator: Process finished with exit code 1

    注意:解决此错误目前只针对Windows系统的电脑: 1.AndroidStudio-->AVDManager(Create Virtual Device): 2.提示AVD启动不了,同时Eve ...

  6. C# 动态创建SQL数据库(一)

    最近在做项目中要求能够要求动态添加数据库并建表.具体思路如下 1 提供数据名,根据数据库创建数据库 2 自定数据库与数据表,提供数据表自定与数据类型创建表 创建sqlhelper类,用于数据库操作 u ...

  7. C#开发邮件收发(同步)

    发邮件界面: 收邮件界面: 先分析邮件发送类 邮件发送类使用smtp协议,这里以QQ邮箱为例 using System; using System.Collections.Generic; using ...

  8. Linux环境GitLab安装与配置

    1.背景 近期公司源代码管理工具从svn转向git,因此要在服务器上部署gitlab,总共部署了两台服务器,一台是Ubuntu server 17.04,一台是Centos7.在部署的过程中遇到不少问 ...

  9. 201621123018《java程序设计》第12周作业总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车. 2.1 简述如何 ...

  10. 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】

    环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...