**线程封闭:**当访问共享的可变数据时,通常需要同步。一种避免同步的方式就是不共享数据。如果仅在单线程内访问数据,就不需要同步,这种技术称为线程封闭(thread confinement)
线程封闭技术一个常见的应用就是JDBC的Connection对象,JDBC规范并没有要求Connection对象必须是线程安全的,在服务器应用程序中,线程从连接池获取一个Connection对象,使用完之后将对象返还给连接池。下面介绍几种线程封闭技术:
1、Ad-hoc线程封闭
Ad-hoc线程封闭是指,维护线程的封闭性的职责完全由程序实现承担,是非常脆弱的,因此在程序中尽量少使用,一般使用更强的线程封闭技术,比如栈封闭或者ThreadLocal类。
2、栈封闭
栈封闭是线程封闭的一种特列,在栈封闭中,只能通过局部变量才能访问对象。局部变量的固有属性之一就是封闭在执行栈中,其他线程无法访问这个栈,栈封闭也称为线程内部使用或者线程局部使用。简单的说就是局部变量。多个线程访问一个方法,此方法中的局部变量都会被拷贝一分儿到线程栈中。所以局部变量是不被多个线程所共享的,也就不会出现并发问题。所以能用局部变量就别用全局的变量,全局变量容易引起并发问题。
比如下面的例子:
1public class Snippet { 2 public int loadTheArk(Collection<Animal> candidates) { 3 SortedSet<Animal> animals; 4 int numPairs = 0; 5 Animal candidate = null; 6 7 // animals被封闭在方法中,不要使它们逸出! 8 animals = new TreeSet<Animal>(new SpeciesGenderComparator()); 9 animals.addAll(candidates); 10 for (Animal a : animals) { 11 if (candidate == null || !candidate.isPotentialMate(a)) 12 candidate = a; 13 else { 14 ark.load(new AnimalPair(candidate, a)); 15 ++numPairs; 16 candidate = null; 17 } 18 } 19 return numPairs; 20 } 21}
在loadTheArk中实例化一个TreeSet对象,并将该对象的一个引用保存到animals中。此时,只有一个引用指向集合animals,这个引用被封闭到局部变量中,因此也被封闭到局部变量中。然而,如果发布了对集合animals(或者该对象中的任何内部数据)的引用,那么封闭性将被破坏,并导致对象animals的逸出。
3、ThreadLocal类
维持线程封闭性的一种更加规范方法是使用ThreadLocal类,这个类能使线程中某个值与保存值的对象关联起来。ThreadLocal类提供了get和set等访问接口或者方法,这些方法为每个使用该变量的线程都存在一份独立的副本,因此get总是放回当前执行线程在调用set设置的最新值。看一下下面代码例子:
1import java.sql.Connection; 2import java.sql.DriverManager; 3import java.sql.SQLException; 4 5public class ConnectionManager { 6 7 private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>() { 8 @Override 9 protected Connection initialValue() { 10 Connection conn = null; 11 try { 12 conn = DriverManager.getConnection( 13 "jdbc:mysql://localhost:3306/test", "username", 14 "password"); 15 } catch (SQLException e) { 16 e.printStackTrace(); 17 } 18 return conn; 19 } 20 }; 21 22 public static Connection getConnection() { 23 return connectionHolder.get(); 24 } 25 26 public static void setConnection(Connection conn) { 27 connectionHolder.set(conn); 28 } 29}
通过调用ConnectionManager.getConnection()方法,每个线程获取到的,都是自己独立拥有的一个的Connection对象副本,第一次获取时,是通过initialValue()方法的返回值来设置值的。通过ConnectionManager.setConnection(Connection conn)方法设置的Connection对象,也只会和当前线程绑定。这样就实现了Connection对象在多个线程中的完全隔离。在Spring容器中管理多线程环境下的Connection对象时,采用的思路和以上代码非常相似。
每个线程是怎么和Connection对象副本绑定的?这个对象副本保存在哪里。当某个线程初次调用ThreadLocal类的get方法时,就会调用initialValue来获取初始值,从概念上看,我们可以将ThreadLocal<T>视为包含了Map<thread, T>对象,其中保存了特定于该线程的值,但是ThreadLocal的实现并非如此,这样只是为了我们方便理解而已。
下面我们来分析一下ThreadLocal****类的源码。ThreadLocal类的方法很简单,只有四个,分别为set,get,remove, initialValue,从字面上我们也能理解这些方法的作用。
**public T get():**返回当前线程所对应的局部变量。
**public void set(T arg0):**设置当前线程局部变量的值。
**public void remove():**将当前线程局部变量的值删除,目的是为了减少内存的占用,该方法是JDK 5.0新增的方法。注意,当线程结束后,对应该线程的局部变量将自动被垃圾回收,所以显式调用该方法清除线程的局部变量并不是必须的操作,但它可以加快内存回收的速度。
protected T initialValue(): 对当线程局部变量进行初始化,并返回该初始值。是protected 属性,显然是让子类进行对其覆盖重写的,只有第一次调用set和get方法时才调用。
下面我们对这四个方法的源码进行分析,看看ThreadLocal类是如何实现这种“为每个线程提供不同的变量拷贝”。
3.1 set方法
以下是set方法的源码
1/** 2 * Sets the current thread's copy of this thread-local variable 3 * to the specified value. Most subclasses will have no need to 4 * override this method, relying solely on the {@link #initialValue} 5 * method to set the values of thread-locals. 6 * 7 * @param value the value to be stored in the current thread's copy of 8 * this thread-local. 9 */ 10 public void set(T value) { 11 Thread t = Thread.currentThread(); 12 ThreadLocalMap map = getMap(t); 13 if (map != null) 14 map.set(this, value); 15 else 16 createMap(t, value); 17 }
从set方法中可以看到,首先获取当前线程:Thread arg1 = Thread.currentThread();
再获取当前线程的ThreadLocalMap:ThreadLocal.ThreadLocalMap arg2 = this.getMap(arg1);
判断ThreadLocalMap是否为空,不为空,则以键值对的形式设置值,key为this,value就是局部变量的副本,this是当前线程持有的ThreadLocal类实例化对象。
假如为空,则通过createMap方法创建。
我们看下getMap和createMap方法的源码:
1ThreadLocalMap getMap(Thread t) { 2 return t.threadLocals; 3 } 4void createMap(Thread t, T firstValue) { 5 t.threadLocals = new ThreadLocalMap(this, firstValue); 6 }
从代码上已经写的非常清楚,每个线程都有自己的局部变量的副本,该副本是存在ThreadLocalMap 中,其中键值就是ThreadLocal类实例化对象。也就是说每个线程都拥有自己的ThreadLocalMap,ThreadLocalMap保存的就是局部变量副本。我们看一下java.lang.Thread源码。
1/* ThreadLocal values pertaining to this thread. This map is maintained 2 * by the ThreadLocal class. */ 3 ThreadLocal.ThreadLocalMap threadLocals = null;
3.2 get方法
1/** 2 * Returns the value in the current thread's copy of this 3 * thread-local variable. If the variable has no value for the 4 * current thread, it is first initialized to the value returned 5 * by an invocation of the {@link #initialValue} method. 6 * 7 * @return the current thread's value of this thread-local 8 */ 9 public T get() { 10 Thread t = Thread.currentThread(); 11 ThreadLocalMap map = getMap(t); 12 if (map != null) { 13 ThreadLocalMap.Entry e = map.getEntry(this); 14 if (e != null) 15 return (T)e.value; 16 } 17 return setInitialValue(); 18 } 19 20 /** 21 * Variant of set() to establish initialValue. Used instead 22 * of set() in case user has overridden the set() method. 23 * 24 * @return the initial value 25 */ 26 private T setInitialValue() { 27 T value = initialValue(); 28 Thread t = Thread.currentThread(); 29 ThreadLocalMap map = getMap(t); 30 if (map != null) 31 map.set(this, value); 32 else 33 createMap(t, value); 34 return value; 35 }
从代码上看,前两步和set方法是一个样的,分别获取当前线程和当前线程的ThreadLocalMap,第三步判断ThreadLocalMap是否为空,不为空根据this键值获取value,为空调用setInitialValue()方法。
以下是setInitialValue方法代码:
1private T setInitialValue() { 2 Object arg0 = this.initialValue(); 3 Thread arg1 = Thread.currentThread(); 4 ThreadLocal.ThreadLocalMap arg2 = this.getMap(arg1); 5 if (arg2 != null) { 6 arg2.set(this, arg0); 7 } else { 8 this.createMap(arg1, arg0); 9 } 10 return arg0; 11}
在setInitialValue里调用了initialValue()方法,也就是子类要重写覆盖的方法,对应上面的例子的代码是:
1protected Connection initialValue() { 2 Connection conn = null; 3 try { 4 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password"); 5 } catch (SQLException e) { 6 e.printStackTrace(); 7 } 8 return conn; 9}
然后获取当前线程和当前线程的ThreadLocalMap,ThreadLocalMap为空则调用createMap,否则调用set方法。
3.3 总结
ThreadLocalMap对象是以this指向的ThreadLocal对象为键进行查找的,这当然和前面set()方法的代码是相呼应的。
进一步地,我们可以创建不同的ThreadLocal实例来实现多个变量在不同线程间的访问隔离,为什么可以这么做?因为不同的ThreadLocal对象作为不同键,当然也可以在线程的ThreadLocalMap对象中设置不同的值了。通过ThreadLocal对象,在多线程中共享一个值和多个值的区别,就像你在一个HashMap对象中存储一个键值对和多个键值对一样,仅此而已。
也就说,每个线程都有一个ThreadLocalMap,该线程访问到某个局部变量,且该局部变量是用ThreadLocal类进行声明时,该线程就会new ThreadLocal(),然后将该ThreadLocal类的对象作为key值,所对应的局部变量作为value值保存到ThreadLocalMap中。当线程访问多个ThreadLocal类进行声明局部变量时,在ThreadLocalMap中就有多个键值对。而每个线程都有自己的ThreadLocalMap,从而达到隔离的目的了。
当某个线程终止后,该线程里的ThreadLocalMap也被回收了,所以完全不用担心内存泄漏的问题。
假如多线程访问的对象实例是单例的,或者说只能创建一个,那就老老实实的使用同步机制(synchronized)了.