让该值和使用它的线程绑定,当然每一个线程都可以独立地改变它绑定的值
主要函数
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Returns the value of this variable for the current thread.
返回当前线程的线程局部变量副本
|
||||||||||
|
Removes the entry for this variable in the current thread.
如果我们想把ThreadLocal所绑定的对象的引用清空,请不要粗暴的把ThreadLocal设置null,而应该调用remove()方法
|
||||||||||
|
value)
Sets the value of this variable for the current thread.
设置当前线程的线程局部变量副本的值
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Provides the initial value of this variable for the current thread.
该方法返回当前线程在该线程局部变量的初始值。这个方法是一个延迟调用方法,在一个线程第1次调用get()且此时还没调用set(Object)时才执行,并且仅执行1次。ThreadLocal中返回的是null。该方法是一个protected的方法,它主要是为设置局部变量的初始值提供方便。
|
ThreadLocal是如何做到让每一个线程和一个值绑定的呢?
其实实现的思路很简单,在ThreadLocal类中有一个Map,用于存储每一个线程的变量的副本。
比如下面的示例实现:
示例1:
public class ThreadLocal {
private Map values = Collections.synchronizedMap(new HashMap());
public Object get() {
Thread curThread = Thread.currentThread();
Object o = values.get(curThread);
if (o == null && !values.containsKey(curThread)) {
o = initialValue();
values.put(curThread, o);
}
return o;
}
public void set(Object newValue) {
values.put(Thread.currentThread(), newValue);
}
public Object initialValue() {
return null;
}
}
因为不同的线程不可能对HashMap的同一项进行操作
在J
下面是去掉了注释后ThreadLocal的代码
public class ThreadLocal<T> {
private final int threadLocalHashCode = nextHashCode();
private static AtomicInteger nextHashCode =
new AtomicInteger();
private static int nextHashCode() {
return nextHashCode.getAndAdd(HASH_INCREMENT);
}
protected T initialValue() {
return null;
}
public ThreadLocal() {
}
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}
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;
}
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
return new ThreadLocalMap(parentMap);
}
T childValue(T parentValue) {
throw new UnsupportedOperationException();
}
注意2:ThreadLocalMap就在ThreadLocal中,但是它并没有使用HashMap,它使用的算法有复杂,没看明白
注意3:
ThreadLocalMap中就是对hashcode取余的方式得到哈希值的。
int len = tab.length;
int i = key.hashCode & (len-1);
因为
/**
* The initial capacity -- MUST be a power of two.
*/
private static final int INITIAL_CAPACITY = 16;
所以上面的"int i = key.hashCode & (len-1);"相当于"int i = key.hashCode %len;"
虽然"HASH_INCREMENT"不是个素数,但是它却不能被tab.length(它等于2的n此方且大于等于16)被整除的。
static class Entry extends WeakReference<ThreadLocal> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal k, Object v) {
super(k);
value = v;
}
}
如果希望线程局部变量初始化其它值,那么需要自己实现ThreadLocal的子类并重写initialValue()该方法,
通常使用一个内部匿名类对ThreadLocal进行子类化,比如下面的例子,SerialNum类为每一个类分配一个序号:
Java代码
public class SerialNum { :
// The next serial number to be assigned
private static int nextSerialNum = 0;
private static ThreadLocal serialNum = new ThreadLocal() {
protected synchronized Object initialValue() {
return new Integer(nextSerialNum++);
}
};
public static int get() {
return ((Integer) (serialNum.get())).intValue();
}
}
SerialNum类的使用将非常地简单,因为get()方法是static的,所以在需要获取当前线程的序号时,简单地调用:
Java代码
int serial = SerialNum.get();
即可。
Hibernate的文档中关于使ThreadLocal管理多线程访问的部分。
具体代码如下
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() {
Session s = (Session)session.get();
//open a new session,if this session has none
if(s == null){
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
我们逐行分析
1。 初始化一个ThreadLocal对象,ThreadLocal有三个成员方法 get()、set()、initialvalue()。
如果不初始化initialvalue,则initialvalue返回null。
3。 session的get根据当前线程返回其对应的线程内部变量,也就是我们需要的net.sf.hibernate.Session(相当于对应每个数据库连接).
多线程情况下共享数据库链接是不安全的。ThreadLocal保证了每个线程都有自己的s(数据库连接)。
5。如果是该线程初次访问,自然,s(数据库连接)会是null,接着创建一个Session,具体就是行6。
6。创建一个数据库连接实例 s
7。保存该数据库连接s到ThreadLocal中。
8。如果当前线程已经访问过数据库了,则从session中get()就可以获取该线程上次获取过的连接实例。
当要给线程初始化一个特殊值时,需要自己实现ThreadLocal的子类并重写该方法,
通常使用一个内部匿名类对ThreadLocal进行子类化,EasyDBO中创建jdbc连接上下文就是这样做的:
public class JDBCContext{
private static Logger logger = Logger.getLogger(JDBCContext.class);
private DataSource ds;
protected Connection connection;
private boolean isValid = true;
private static ThreadLocal jdbcContext;
private JDBCContext(DataSource ds){
this.ds = ds;
createConnection();
}
public static JDBCContext getJdbcContext(javax.sql.DataSource ds)
{
if(jdbcContext==null)jdbcContext=new JDBCContextThreadLocal(ds);
JDBCContext context = (JDBCContext) jdbcContext.get();
if (context == null) {
context = new JDBCContext(ds);
return context;
}
private static class JDBCContextThreadLocal extends ThreadLocal {
public javax.sql.DataSource ds;
public JDBCContextThreadLocal(javax.sql.DataSource ds)
{
this.ds=ds;
}
protected synchronized Object initialValue() {
return new JDBCContext(ds);
}
}
}
使用单例模式,不同的线程调用getJdbcContext()获得自己的jdbcContext,
都是通过JDBCContextThreadLocal内置子类来获得JDBCContext对象的线程局部变量
同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式; 而ThreadLocal是隔离多个线程的数据共享,从根本上就不在多个线程之间共享资源(变量),这样当然不需要对多个线程进行同步了。
所以,如果你需要进行多个线程之间进行通信,则使用同步机制;如果需要
如果我们想把ThreadLocal所绑定的对象的引用清空,请不要粗暴的把ThreadLocal引用设置null,而应该调用remove()方法。否则会造成内存泄露。关于此的更多内容请参考《ThreadLocal的内存泄露》