书店网站建设方案,建网站的论坛,建湖做网站找哪家好,推广公司起名想必很多朋友对 ThreadLocal并不陌生#xff0c;今天我们就来一起探讨下ThreadLocal的使用方法和实现原理。首先#xff0c;本文先谈一下对ThreadLocal的理 解#xff0c;然后根据ThreadLocal类的源码分析了其实现原理和使用需要注意的地方#xff0c;最后给出了两个应用场…想必很多朋友对 ThreadLocal并不陌生今天我们就来一起探讨下ThreadLocal的使用方法和实现原理。首先本文先谈一下对ThreadLocal的理 解然后根据ThreadLocal类的源码分析了其实现原理和使用需要注意的地方最后给出了两个应用场景。 以下是本文目录大纲 一.对ThreadLocal的理解 二.深入解析ThreadLocal类 三.ThreadLocal的应用场景 若有不正之处请多多谅解并欢迎批评指正。 请尊重作者劳动成果转载请标明原文链接 http://www.cnblogs.com/dolphin0520/p/3920407.html 一.对ThreadLocal的理解 ThreadLocal很多地方叫做线程本地变量也有些地方叫做线程本地存储其实意思差不多。可能很多朋友都知道ThreadLocal为变量在每个线程中都创建了一个副本那么每个线程可以访问自己内部的副本变量。 这句话从字面上看起来很容易理解但是真正理解并不是那么容易。 我们还是先来看一个例子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class ConnectionManager { private static Connection connect null; public static Connection openConnection() { if(connect null){ connect DriverManager.getConnection(); } return connect; } public static void closeConnection() { if(connect!null) connect.close(); } } 假设有这样一个数据库链接管理类这段代码在单线程中使用是没有任何问题的但是如果在多线程中使用呢很显然在多线程中使用会存在线程安 全问题第一这里面的2个方法都没有进行同步很可能在openConnection方法中会多次创建connect第二由于connect是共享 变量那么必然在调用connect的地方需要使用到同步来保障线程安全因为很可能一个线程在使用connect进行数据库操作而另外一个线程调用 closeConnection关闭链接。 所以出于线程安全的考虑必须将这段代码的两个方法进行同步处理并且在调用connect的地方需要进行同步处理。 这样将会大大影响程序执行效率因为一个线程在使用connect进行数据库操作的时候其他线程只有等待。 那么大家来仔细分析一下这个问题这地方到底需不需要将connect变量进行共享事实上是不需要的。假如每个线程中都有一个 connect变量各个线程之间对connect变量的访问实际上是没有依赖关系的即一个线程不需要关心其他线程是否对这个connect进行了修改 的。 到这里可能会有朋友想到既然不需要在线程之间共享这个变量可以直接这样处理在每个需要使用数据库连接的方法中具体使用时才创建数据库链接然后在方法调用完毕再释放这个连接。比如下面这样 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class ConnectionManager { private Connection connect null; public Connection openConnection() { if(connect null){ connect DriverManager.getConnection(); } return connect; } public void closeConnection() { if(connect!null) connect.close(); } } class Dao{ public void insert() { ConnectionManager connectionManager new ConnectionManager(); Connection connection connectionManager.openConnection(); //使用connection进行操作 connectionManager.closeConnection(); } } 这样处理确实也没有任何问题由于每次都是在方法内部创建的连接那么线程之间自然不存在线程安全问题。但是这样会有一个致命的影响导致服 务器压力非常大并且严重影响程序执行性能。由于在方法中需要频繁地开启和关闭数据库连接这样不尽严重影响程序执行效率还可能导致服务器压力巨大。 那么这种情况下使用ThreadLocal是再适合不过的了因为ThreadLocal在每个线程中对该变量会创建一个副本即每个线程内部 都会有一个该变量且在线程内部任何地方都可以使用线程之间互不影响这样一来就不存在线程安全问题也不会严重影响程序执行性能。 但是要注意虽然ThreadLocal能够解决上面说的问题但是由于在每个线程中都创建了副本所以要考虑它对资源的消耗比如内存的占用会比不使用ThreadLocal要大。 二.深入解析ThreadLocal类 在上面谈到了对ThreadLocal的一些理解那我们下面来看一下具体ThreadLocal是如何实现的。 先了解一下ThreadLocal类提供的几个方法 1 2 3 4 public T get() { } public void set(T value) { } public void remove() { } protected T initialValue() { } get()方法是用来获取ThreadLocal在当前线程中保存的变量副本set()用来设置当前线程中变量的副本remove()用 来移除当前线程中变量的副本initialValue()是一个protected方法一般是用来在使用时进行重写的它是一个延迟加载方法下面会 详细说明。 首先我们来看一下ThreadLocal类是如何为每个线程创建一个变量的副本的。 先看下get方法的实现 第一句是取得当前线程然后通过getMap(t)方法获取到一个mapmap的类型为ThreadLocalMap。然后接着下面获取到key,value键值对注意这里获取键值对传进去的是 this而不是当前线程t。 如果获取成功则返回value值。 如果map为空则调用setInitialValue方法返回value。 我们上面的每一句来仔细分析 首先看一下getMap方法中做了什么 可能大家没有想到的是在getMap中是调用当期线程t返回当前线程t中的一个成员变量threadLocals。 那么我们继续取Thread类中取看一下成员变量threadLocals是什么 实际上就是一个ThreadLocalMap这个类型是ThreadLocal类的一个内部类我们继续取看ThreadLocalMap的实现 可以看到ThreadLocalMap的Entry继承了WeakReference并且使用ThreadLocal作为键值。 然后再继续看setInitialValue方法的具体实现 很容易了解就是如果map不为空就设置键值对为空再创建Map看一下createMap的实现 至此可能大部分朋友已经明白了ThreadLocal是如何为每个线程创建变量的副本的 首先在每个线程Thread内部有一个ThreadLocal.ThreadLocalMap类型的成员变量threadLocals这个 threadLocals就是用来存储实际的变量副本的键值为当前ThreadLocal变量value为变量副本即T类型的变量。 初始时在Thread里面threadLocals为空当通过ThreadLocal变量调用get()方法或者set()方法就会对 Thread类中的threadLocals进行初始化并且以当前ThreadLocal变量为键值以ThreadLocal要保存的副本变量为 value存到threadLocals。 然后在当前线程里面如果要使用副本变量就可以通过get方法在threadLocals里面查找。 下面通过一个例子来证明通过ThreadLocal能达到在每个线程中创建变量副本的效果 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class Test { ThreadLocalLong longLocal new ThreadLocalLong(); ThreadLocalString stringLocal new ThreadLocalString(); 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 Test test new Test(); test.set(); System.out.println(test.getLong()); System.out.println(test.getString()); Thread thread1 new Thread(){ public void run() { test.set(); System.out.println(test.getLong()); System.out.println(test.getString()); }; }; thread1.start(); thread1.join(); System.out.println(test.getLong()); System.out.println(test.getString()); } } 这段代码的输出结果为 从这段代码的输出结果可以看出在main线程中和thread1线程中longLocal保存的副本值和stringLocal保存的副本值都不一样。最后一次在main线程再次打印副本值是为了证明在main线程中和thread1线程中的副本值确实是不同的。 总结一下 1实际的通过ThreadLocal创建的副本是存储在每个线程自己的threadLocals中的 2为何threadLocals的类型ThreadLocalMap的键值为ThreadLocal对象因为每个线程中可有多个threadLocal变量就像上面代码中的longLocal和stringLocal 3在进行get之前必须先set否则会报空指针异常 如果想在get之前不需要调用set就能正常访问的话必须重写initialValue()方法。 因为在上面的代码分析过程中我们发现如果没有先set的话即在map中查找不到对应的存储则会通过调用setInitialValue方法返回i 而在setInitialValue方法中有一个语句是T value initialValue() 而默认情况下initialValue方法返回的是null。 看下面这个例子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public class Test { ThreadLocalLong longLocal new ThreadLocalLong(); ThreadLocalString stringLocal new ThreadLocalString(); 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 Test test new Test(); System.out.println(test.getLong()); System.out.println(test.getString()); Thread thread1 new Thread(){ public void run() { test.set(); System.out.println(test.getLong()); System.out.println(test.getString()); }; }; thread1.start(); thread1.join(); System.out.println(test.getLong()); System.out.println(test.getString()); } } 在main线程中没有先set直接get的话运行时会报空指针异常。 但是如果改成下面这段代码即重写了initialValue方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 public class Test { ThreadLocalLong longLocal new ThreadLocalLong(){ protected Long initialValue() { return Thread.currentThread().getId(); }; }; ThreadLocalString stringLocal new ThreadLocalString(){; protected String initialValue() { return Thread.currentThread().getName(); }; }; 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 Test test new Test(); test.set(); System.out.println(test.getLong()); System.out.println(test.getString()); Thread thread1 new Thread(){ public void run() { test.set(); System.out.println(test.getLong()); System.out.println(test.getString()); }; }; thread1.start(); thread1.join(); System.out.println(test.getLong()); System.out.println(test.getString()); } } 就可以直接不用先set而直接调用get了。 三.ThreadLocal的应用场景 最常见的ThreadLocal使用场景为 用来解决 数据库连接、Session管理等。 如 1 2 3 4 5 6 7 8 9 10 private static ThreadLocalConnection connectionHolder new ThreadLocalConnection() { public Connection initialValue() { return DriverManager.getConnection(DB_URL); } }; public static Connection getConnection() { return connectionHolder.get(); } 下面这段代码摘自 http://www.iteye.com/topic/103804 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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; } 参考资料 《深入理解Java虚拟机》 《Java编程思想》 http://ifeve.com/thread-management-10/ http://www.ibm.com/developerworks/cn/java/j-threads/index3.html http://www.iteye.com/topic/103804 http://www.iteye.com/topic/777716 http://www.iteye.com/topic/757478 http://blog.csdn.net/ghsau/article/details/15732053 http://ispring.iteye.com/blog/162982 http://blog.csdn.net/imzoer/article/details/8262101 http://www.blogjava.net/wumi9527/archive/2010/09/10/331654.html http://bbs.csdn.net/topics/380049261 转载来自作者海子 出处http://www.cnblogs.com/dolphin0520/ 转载于:https://www.cnblogs.com/suiyueqiannian/p/5961452.html