ThreadLocal详解
来源:互联网 发布:软件著作权登记公告 编辑:程序博客网 时间:2024/06/16 07:06
ThreadLocal,即线程变量,是一个以ThreadLocal对象为键、任意对象为值的存储结构。这个结构被附带在线程上,也就是说一个线程可以根据一个ThreadLocal对象查询到绑定在这个线程上的一个值。
ThreadLocal源码分析
我们看下ThreadLocal几个关键方法
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(); // 通过getMap(t)方法获取到一个map,map的类型为ThreadLocalMap ThreadLocalMap map = getMap(t); if (map != null) { // 然后接着下面获取到<key,value>键值对,注意这里获取键值对传进去的是 this,而不是当前线程t。 ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) return (T)e.value; } // 如果map为空,则调用setInitialValue方法返回value return setInitialValue(); }
然后我们看下
getMap()
/** * 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; }
在getMap中,是调用当期线程t,返回当前线程t中的一个成员变量threadLocals
那么我们继续取Thread类中看一下成员变量threadLocals:
/* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLocals = null;
实际上就是一个ThreadLocalMap,这个类型是ThreadLocal类的一个内部类,我们继续看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. */ static class Entry extends WeakReference<ThreadLocal> { /** The value associated with this ThreadLocal. */ Object value; Entry(ThreadLocal k, Object v) { super(k); value = v; } }}
可以看到ThreadLocalMap的Entry继承了WeakReference,并且使用ThreadLocal作为键值。然后再继续看setInitialValue方法的具体实现
setInitialValue
/** * Variant of set() to establish initialValue. Used instead * of set() in case user has overridden the set() method. * * @return the initial value */ 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; }
如果map不为空,就设置键值对,为空,再创建Map,看一下createMap的实现
createMap
/** * 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 * @param map the map to store. */ void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); }
下面我们来分析下ThreadLocal如何为每个线程变量创建变量副本的
在每个线程Thread内部有一个ThreadLocal.ThreadLocalMap类型的成员变量threadLocals,这个threadLocals就是用来存储实际的变量副本的,键值为当前ThreadLocal变量,value为变量副本(即T类型的变量)。初始时,在Thread里面,threadLocals为空,当通过ThreadLocal变量调用get()方法或者set()方法,就会对Thread类中的threadLocals进行初始化,并且以当前ThreadLocal变量为键值,以ThreadLocal要保存的副本变量为value,存到threadLocals。
然后在当前线程里面,如果要使用副本变量,就可以通过get方法在threadLocals里面查找。
代码示例
public class ThreadLocalTest { ThreadLocal<Long> id = new ThreadLocal<Long>(); ThreadLocal<String> name = new ThreadLocal<String>(); public void set() { id.set(Thread.currentThread().getId()); name.set(Thread.currentThread().getName()); } public long getId() { return id.get(); } public String getName() { return name.get(); } public static void main(String[] args) { final ThreadLocalTest test = new ThreadLocalTest(); test.set(); System.out.println(test.getId()); System.out.println(test.getName()); Thread thread1 = new Thread() { public void run() { test.set(); System.out.println(test.getId()); System.out.println(test.getName()); }; }; thread1.start(); try { thread1.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(test.getId()); System.out.println(test.getName()); }}
运行结果如下:
1main9Thread-01main
然后再继续看setInitialValue方法的具体实现:
setInitialValue
从这段代码的输出结果可以看出,在main线程中和thread1线程中,id保存的副本值和name保存的副本值都不一样。最后一次在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。
如果我把set去掉
public static void main(String[] args) { final ThreadLocalTest test = new ThreadLocalTest(); System.out.println(test.getId()); System.out.println(test.getName()); Thread thread1 = new Thread() { public void run() { test.set(); System.out.println(test.getId()); System.out.println(test.getName()); }; }; thread1.start(); try { thread1.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(test.getId()); System.out.println(test.getName()); }
运行结果如下:
Exception in thread "main" java.lang.NullPointerException at com.thread.ThreadLocalTest.getId(ThreadLocalTest.java:15) at com.thread.ThreadLocalTest.main(ThreadLocalTest.java:25)
下面我们看一个不用set而是通过重写initialValue()方法
public class ThreadLocalTest { ThreadLocal<Long> id = new ThreadLocal<Long>(){ @Override protected Long initialValue() { return Thread.currentThread().getId(); } }; ThreadLocal<String> name = new ThreadLocal<String>(){ @Override protected String initialValue() { return Thread.currentThread().getName(); } }; public void set() { id.set(Thread.currentThread().getId()); name.set(Thread.currentThread().getName()); } public long getId() { return id.get(); } public String getName() { return name.get(); } public static void main(String[] args) { final ThreadLocalTest test = new ThreadLocalTest(); System.out.println(test.getId()); System.out.println(test.getName()); Thread thread1 = new Thread() { public void run() { System.out.println(test.getId()); System.out.println(test.getName()); }; }; thread1.start(); try { thread1.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(test.getId()); System.out.println(test.getName()); }}
运行结果
1main9Thread-01main
ThreadLocal的应用场景
最常见的ThreadLocal使用场景为 用来解决数据库连接、Session管理等。如
数据库连接
private static ThreadLocal<Connection> connection = new ThreadLocal<Connection>() { public Connection initialValue() { return DriverManager.getConnection(DB_URL); } }; public static Connection getConnection() { return connection .get(); }
session管理
下面来看一个hibernate中典型的ThreadLocal的应用
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; }
ThreadLocal的内存泄露
ThreadLocal不是用来解决对象共享访问问题的,而主要是提供了保持对象的方法和避免参数传递的方便的对象访问方式。
1。每个线程中都有一个自己的ThreadLocalMap类对象,可以将线程自己的对象保持到其中,各管各的,线程可以正确的访问到自己的对象。
2。将一个共用的ThreadLocal静态实例作为key,将不同对象的引用保存到不同线程的ThreadLocalMap中,然后在线程执行的各处通过这个静态ThreadLocal实例的get()方法取得自己线程保存的那个对象,避免了将这个对象作为参数传递的麻烦。
当然ThreadLocal并不能替代同步机制,两者面向的问题领域不同。同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式;而ThreadLocal是隔离多个线程的数据共享,从根本上就不在多个线程之间共享资源(变量),这样当然不需要对多个线程进行同步了。所以,如果你需要进行多个线程之间进行通信,则使用同步机制;如果需要隔离多个线程之间的共享冲突,可以使用ThreadLocal,这将极大地简化我们的程序,使程序更加易读、简洁。ThreadLocal类为各线程提供了存放局部变量的场所。
每个Thread实例都具备一个ThreadLocal的map,以ThreadLocal Instance为key,以绑定的Object为Value。而这个map不是普通的map,它是在ThreadLocal中定义的,它和普通map的最大区别就是它的Entry是针对ThreadLocal弱引用的,即当外部ThreadLocal引用为空时,map就可以把ThreadLocal交给GC回收,从而得到一个null的key。
这个threadlocal内部的map在Thread实例内部维护了ThreadLocal Instance和bind value之间的关系,这个map有threshold,当超过threshold时,map会首先检查内部的ThreadLocal(前文说过,map是弱引用可以释放)是否为null,如果存在null,那么释放引用给gc,这样保留了位置给新的线程。如果不存在slate threadlocal,那么double threshold。除此之外,还有两个机会释放掉已经废弃的threadlocal占用的内存,一是当hash算法得到的table index刚好是一个null key的threadlocal时,直接用新的threadlocal替换掉已经废弃的。另外每次在map中新建一个entry时(即没有和用过的或未清理的entry命中时),会调用cleanSomeSlots来遍历清理空间。此外,当Thread本身销毁时,这个map也一定被销毁了(map在Thread之内),这样内部所有绑定到该线程的ThreadLocal的Object Value因为没有引用继续保持,所以被销毁。
从上可以看出Java已经充分考虑了时间和空间的权衡,但是因为置为null的threadlocal对应的Object Value无法及时回收。map只有到达threshold时或添加entry时才做检查,不似gc是定时检查,不过我们可以手工轮询检查,显式调用map的remove方法,及时的清理废弃的threadlocal内存。需要说明的是,只要不往不用的threadlocal中放入大量数据,问题不大,毕竟还有回收的机制。
综上,废弃threadlocal占用的内存会在3中情况下清理:
1 thread结束,那么与之相关的threadlocal value会被清理
2 GC后,thread.threadlocals(map) threshold超过最大值时,会清理
3 GC后,thread.threadlocals(map) 添加新的Entry时,hash算法没有命中既有Entry时,会清理
那么何时会“内存泄露”?当Thread长时间不结束,存在大量废弃的ThreadLocal,而又不再添加新的ThreadLocal(或新添加的ThreadLocal恰好和一个废弃ThreadLocal在map中命中)时。
(摘自http://www.cnblogs.com/549294286/p/3880205.html)
总结
ThreadLocal不是用来解决对象共享访问问题的,而主要是提供了保持对象的方法和避免参数传递的方便的对象访问方式。
1。每个线程中都有一个自己的ThreadLocalMap类对象,可以将线程自己的对象保持到其中,各管各的,线程可以正确的访问到自己的对象。
2。将一个共用的ThreadLocal静态实例作为key,将不同对象的引用保存到不同线程的ThreadLocalMap中,然后在线程执行的各处 通过这个静态ThreadLocal实例的get()方法取得自己线程保存的那个对象,避免了将这个对象作为参数传递的麻烦。
当然ThreadLocal并不能替代同步机制,两者面向的问题领域不同。同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式;而ThreadLocal是隔离多个线程的数据共享,从根本上就不在多个线程之间共享资源(变量),这样当然不需要对多个线程进行同步了。所以,如果你需要进行多个线程之间进行通信,则使用同步机制;如果需要隔离多个线程之间的共享冲突,可以使用ThreadLocal,这将极大地简化我们的程序,使程序更加易读、简洁。ThreadLocal类为各线程提供了存放局部变量的场所。
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal 详解
- ThreadLocal 详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- ThreadLocal详解
- IT基础8
- IT基础9
- 修复“勒索漏洞”,“想哭漏洞”,“445端口漏洞”,修复微软“KB4012212”补丁的一些办法
- 转:svn和git的区别,maven是什么
- JTS空间分析工具包(GIS开源)学习 JAVA
- ThreadLocal详解
- IT基础10
- Java中Properties文件读取工具类
- [leetcode: Python]475.Heaters
- 将资源文件发布到Linux(以CentOS为例)服务器(虚拟机)上
- /usr/bin/ld: cannot find -lnvcuvid
- Ajax简单实例
- IT基础11
- DALI调光方案