全屏 单页网站,关于加强教体局网站建设,易观数据app排行,国内的搜索引擎排名前言 今天要研究的是ThreadLocal#xff0c;这个我在一年前学习JavaWeb基础的时候接触过一次#xff0c;当时在baidu搜出来的第一篇博文ThreadLocal#xff0c;在评论下很多开发者认为那博主理解错误#xff0c;给出了很多有关的链接来指正(可原博主可能没上博客了#xf… 前言 今天要研究的是ThreadLocal这个我在一年前学习JavaWeb基础的时候接触过一次当时在baidu搜出来的第一篇博文ThreadLocal在评论下很多开发者认为那博主理解错误给出了很多有关的链接来指正(可原博主可能没上博客了一直没做修改)。我也去学习了一番可惜的是当时还没有记录的习惯直到现在仅仅记住了一些当时学过的皮毛。 因此做一些技术的记录是很重要的同时ThreadLocal也是面试非常常见的面试题对Java开发者而言也是一个必要掌握的知识点 当然了如果我有写错的地方请大家多多包涵欢迎在评论下留言指正 一、什么是ThreadLocal 声明本文使用的是JDK 1.8 首先我们来看一下JDK的文档介绍
/*** This class provides thread-local variables. These variables differ from* their normal counterparts in that each thread that accesses one (via its* {code get} or {code set} method) has its own, independently initialized* copy of the variable. {code ThreadLocal} instances are typically private* static fields in classes that wish to associate state with a thread (e.g.,* a user ID or Transaction ID).* * pFor example, the class below generates unique identifiers local to each* thread.* A threads id is assigned the first time it invokes {code ThreadId.get()}* and remains unchanged on subsequent calls.*/ 结合我的总结可以这样理解ThreadLocal提供了线程的局部变量每个线程都可以通过set()和get()来对这个局部变量进行操作但不会和其他线程的局部变量进行冲突实现了线程的数据隔离。 简要言之往ThreadLocal中填充的变量属于当前线程该变量对其他线程而言是隔离的。 二、为什么要学习ThreadLocal 从上面可以得出ThreadLocal可以让我们拥有当前线程的变量那这个作用有什么用呢 2.1管理Connection 最典型的是管理数据库的Connection当时在学JDBC的时候为了方便操作写了一个简单数据库连接池需要数据库连接池的理由也很简单频繁创建和关闭Connection是一件非常耗费资源的操作因此需要创建数据库连接池 那么数据库连接池的连接怎么管理呢我们交由ThreadLocal来进行管理。为什么交给它来管理呢ThreadLocal能够实现当前线程的操作都是用同一个Connection保证了事务 当时候写的代码
public class DBUtil {//数据库连接池private static BasicDataSource source;//为不同的线程管理连接private static ThreadLocalConnection local;static {try {//加载配置文件Properties properties new Properties();//获取读取流InputStream stream DBUtil.class.getClassLoader().getResourceAsStream(连接池/config.properties);//从配置文件中读取数据properties.load(stream);//关闭流stream.close();//初始化连接池source new BasicDataSource();//设置驱动source.setDriverClassName(properties.getProperty(driver));//设置urlsource.setUrl(properties.getProperty(url));//设置用户名source.setUsername(properties.getProperty(user));//设置密码source.setPassword(properties.getProperty(pwd));//设置初始连接数量source.setInitialSize(Integer.parseInt(properties.getProperty(initsize)));//设置最大的连接数量source.setMaxActive(Integer.parseInt(properties.getProperty(maxactive)));//设置最长的等待时间source.setMaxWait(Integer.parseInt(properties.getProperty(maxwait)));//设置最小空闲数source.setMinIdle(Integer.parseInt(properties.getProperty(minidle)));//初始化线程本地local new ThreadLocal();} catch (IOException e) {e.printStackTrace();}}public static Connection getConnection() throws SQLException {if(local.get()!null){return local.get();}else{//获取Connection对象Connection connection source.getConnection();//把Connection放进ThreadLocal里面local.set(connection);//返回Connection对象return connection;}}//关闭数据库连接public static void closeConnection() {//从线程中拿到Connection对象Connection connection local.get();try {if (connection ! null) {//恢复连接为自动提交connection.setAutoCommit(true);//这里不是真的把连接关了,只是将该连接归还给连接池connection.close();//既然连接已经归还给连接池了,ThreadLocal保存的Connction对象也已经没用了local.remove();}} catch (SQLException e) {e.printStackTrace();}}} 同样的Hibernate对Connection的管理也是采用了相同的手法(使用ThreadLocal当然了Hibernate的实现是更强大的) 2.2避免一些参数传递 避免一些参数的传递的理解可以参考一下Cookie和Session 每当我访问一个页面的时候浏览器都会帮我们从硬盘中找到对应的Cookie发送过去。浏览器是十分聪明的不会发送别的网站的Cookie过去只带当前网站发布过来的Cookie过去浏览器就相当于我们的ThreadLocal它仅仅会发送我们当前浏览器存在的Cookie(ThreadLocal的局部变量)不同的浏览器对Cookie是隔离的(Chrome,Opera,IE的Cookie是隔离的【在Chrome登陆了在IE你也得重新登陆】)同样地线程之间ThreadLocal变量也是隔离的.... 那上面避免了参数的传递了吗其实是避免了。Cookie并不是我们手动传递过去的并不需要写input name cookie/来进行传递参数... 在编写程序中也是一样的日常中我们要去办理业务可能会有很多地方用到身份证各类证件每次我们都要掏出来很麻烦 // 咨询时要用身份证学生证房产证等等....public void consult(IdCard idCard,StudentCard studentCard,HourseCard hourseCard){}// 办理时还要用身份证学生证房产证等等....public void manage(IdCard idCard,StudentCard studentCard,HourseCard hourseCard) {}//......而如果用了ThreadLocal的话ThreadLocal就相当于一个机构ThreadLocal机构做了记录你有那么多张证件。用到的时候就不用自己掏了问机构拿就可以了。 在咨询时的时候就告诉机构来把我的身份证、房产证、学生证通通给他。在办理时又告诉机构来把我的身份证、房产证、学生证通通给他。... // 咨询时要用身份证学生证房产证等等....public void consult(){threadLocal.get();}// 办理时还要用身份证学生证房产证等等....public void takePlane() {threadLocal.get();}这样是不是比自己掏方便多了。 当然了ThreadLocal可能还会有其他更好的作用如果知道的同学可在评论留言哦 三、ThreadLocal实现的原理 想要更好地去理解ThreadLocal那就得翻翻它是怎么实现的了 声明本文使用的是JDK 1.8 首先我们来看一下ThreadLocal的set()方法因为我们一般使用都是new完对象就往里边set对象了 public void set(T value) {// 得到当前线程对象Thread t Thread.currentThread();// 这里获取ThreadLocalMapThreadLocalMap map getMap(t);// 如果map存在则将ThreadLocal作为key要存储的对象作为value存到map里面去if (map ! null)map.set(this, value);elsecreateMap(t, value);}上面有个ThreadLocalMap我们去看看这是什么
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.*/static class Entry extends WeakReferenceThreadLocal? {/** The value associated with this ThreadLocal. */Object value;Entry(ThreadLocal? k, Object v) {super(k);value v;}}//....很长
} 通过上面我们可以发现的是ThreadLocalMap是ThreadLocal的一个内部类。用Entry类来进行存储 我们的值都是存储到这个Map上的key是当前ThreadLocal对象 如果该Map不存在则初始化一个 void createMap(Thread t, T firstValue) {t.threadLocals new ThreadLocalMap(this, firstValue);} 如果该Map存在则从Thread中获取 /*** Get the map associated with a ThreadLocal. Overridden in* InheritableThreadLocal.** param t the current thread* return the map*/ThreadLocalMap getMap(Thread t) {return t.threadLocals;}Thread维护了ThreadLocalMap变量 /* ThreadLocal values pertaining to this thread. This map is maintained* by the ThreadLocal class. */ThreadLocal.ThreadLocalMap threadLocals null 从上面又可以看出ThreadLocalMap是在ThreadLocal中使用内部类来编写的但对象的引用是在Thread中 于是我们可以总结出Thread为每个线程维护了ThreadLocalMap这么一个Map而ThreadLocalMap的key是LocalThread对象本身value则是要存储的对象 有了上面的基础我们看get()方法就一点都不难理解了 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();} 3.1ThreadLocal原理总结 每个Thread维护着一个ThreadLocalMap的引用ThreadLocalMap是ThreadLocal的内部类用Entry来进行存储调用ThreadLocal的set()方法时实际上就是往ThreadLocalMap设置值key是ThreadLocal对象值是传递进来的对象调用ThreadLocal的get()方法时实际上就是往ThreadLocalMap获取值key是ThreadLocal对象 ThreadLocal本身并不存储值它只是作为一个key来让线程从ThreadLocalMap获取value。正因为这个原理所以ThreadLocal能够实现“数据隔离”获取当前线程的局部变量值不受其他线程影响 四、避免内存泄露 我们来看一下ThreadLocal的对象关系引用图 ThreadLocal内存泄漏的根源是由于ThreadLocalMap的生命周期跟Thread一样长如果没有手动删除对应key就会导致内存泄漏而不是因为弱引用。 想要避免内存泄露就要手动remove()掉 五、总结 ThreadLocal这方面的博文真的是数不胜数随便一搜就很多很多站在前人的肩膀上总结了这篇博文 最后要记住的是:ThreadLocal设计的目的就是为了能够在当前线程中有属于自己的变量并不是为了解决并发或者共享变量的问题 如果看得不够过瘾觉得不够深入的同学可参考下面的链接很多的博主还开展了一些扩展知识我就不一一展开了 参考博文 http://blog.xiaohansong.com/2016/08/06/ThreadLocal-memory-leak/https://www.cnblogs.com/zhangjk1993/archive/2017/03/29/6641745.html#_label2http://www.cnblogs.com/dolphin0520/p/3920407.htmlhttp://www.cnblogs.com/dolphin0520/p/3920407.htmlhttp://www.iteye.com/topic/103804https://www.cnblogs.com/xzwblog/p/7227509.htmlhttps://blog.csdn.net/u012834750/article/details/71646700https://blog.csdn.net/winwill2012/article/details/71625570https://juejin.im/post/5a64a581f265da3e3b7aa02d 如果文章有错的地方欢迎指正大家互相交流。习惯在微信看技术文章想要获取更多的Java资源的同学可以关注微信公众号:Java3y 更多的文章可往文章的目录导航