Java中除了通过共享变量,还可以通过ThreadLocal来实现多线程访问的问题。按字面理解,ThreadLocal相当于线程的局部变量 。在每个线程中为目标对象创建一个副本,这样自然避免了共享变量参数传递带来的线程安全问题。我们来做个测试:
package test.thread;
public class ThreadLocalTest1 {
private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();
public static Object getInstance() {
Object o = threadLocal.get();
if (null == o) {
o = new Object();
threadLocal.set(o);
}
return o;
}
public static void main(String[] args) {
Runnable threadImp = new Runnable() {
@Override
public void run() {
Object o = getInstance();
System.out.println(o);
}
};
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(threadImp);
threads[i].start();
}
}
}
最后打印结果:
java.lang.Object@6f92c766
java.lang.Object@4f9bbd86
java.lang.Object@d6132c4
java.lang.Object@d6132c4
java.lang.Object@5cb08ba7
java.lang.Object@130a7be0
java.lang.Object@3918d722
java.lang.Object@4b0bc3c9
java.lang.Object@1453ecec
java.lang.Object@11e78461
上面结果显然印证了前面所述。那ThreadLocal是如何实现的呢?我们来看来下源码:
/**
* 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();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}
/**
* Sets the current thread's copy of this thread-local variable
* to the specified value. Most subclasses will have no need to
* override this method, relying solely on the {@link #initialValue}
* method to set the values of thread-locals.
*
* @param value the value to be stored in the current thread's copy of
* this thread-local.
*/
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
/**
* 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;
}
从get方法可以看出ThreadLocal所属的线程会维系一个ThreadLocalMap,最终取出的对象就存放在key为当前ThreadLocal对象的entry中,默认返回个null值。
ThreadLocal多用于多线程创建线程内单例,如Session、Connection等。Spring、Hibernate中就有很多单例使用ThreadLocal实现。
参考: