1.ThreadLocal
ThreadLocal是解决线程安全问题一个很好的思路,它通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题。
ThreadLocal是每个Thread都绑定一个Map,线程之间不会互相干扰。
2.什么是ThreadLocal
ThreadLocal有点类似于Map类型的数据变量。ThreadLocal类型的变量每个线程都有自己的一个副本,某个线程对这个变量的修改不会影响其他线程副本的值,可以说ThreadLocal为我们提供了一个保证线程安全的新思路。需要注意的是一个ThreadLocal变量,其中只能set一个值。
3.使用场景
在Java的多线程编程中,为保证多个线程对共享变量的安全访问,通常会使用synchronized来保证同一时刻只有一个线程对共享变量进行操作。这种情况下可以将类变量放到ThreadLocal类型的对象中,使变量在每个线程中都有独立拷贝,不会出现一个线程读取变量时而被另一个线程修改的现象。最常见的ThreadLocal使用场景为用来解决数据库连接、Session管理等。在下面会例举几个场景。
ThreadLocal<String> contextHolder = new ThreadLocal();
contextHolder.set("username-donald-trump");
String name = contextHolder.get();
4. ThreadLocal的内部原理
ThreadLocal类中提供了几个方法:
1.public T get() { }
2.public void set(T value) { }
3.public void remove() { }
4.protected T initialValue(){ }
get()方法是用来获取ThreadLocal在当前线程中保存的变量副本,set()用来设置当前线程中变量的副本,remove()用来移除当前线程中变量的副本,initialValue()是一个protected方法,一般是用来在使用时进行重写的,它是一个延迟加载方法,下面会详细说明。
4.1.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();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return 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;
}
第一句是取得当前线程,然后通过getMap(t)方法获取到一个map,map的类型为ThreadLocalMap。然后接着下面获取到<key,value>键值对,注意这里获取键值对传进去的是 this,而不是当前线程t。 如果获取成功,则返回value值。如果map为空,则调用setInitialValue方法返回value。
getMap(t)做了些什么
* 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是什么?继续查看源码
* 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,这个类型是ThreadLocal类的一个内部类,继续看ThreadLocalMap的实现。
测试
示例代码:
import java.util.Random;
public class ThreadLocalTest {
public static void main(String[] args) {
WatchDogWorker sharedRunnableInstance = new WatchDogWorker();
Thread aWorkerThread = new Thread(sharedRunnableInstance);
Thread bWorkerThread = new Thread(sharedRunnableInstance);
aWorkerThread.start();
bWorkerThread.start();
}
public static class WatchDogWorker implements Runnable {
private static ThreadLocal threadLocal = new ThreadLocal();
private static Random random = new Random();
@Override
public void run() {
try {
threadLocal.set(random.nextInt());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadLocal value:" + threadLocal.get());
} finally {
threadLocal.remove();
}
}
}
}
5.简单总结
- 每个Thread对象内部都有一个ThreadLoacalMap的成员变量,这个变量类似一个Map类型,其中key为我们定义的ThreadLocal变量的this引用,value则为我们使用set方法设置的值;
- 如果线程不消亡,在ThreadLocalMap中存放的ThreadLocal实例对象可能一直不会清除,所以当我们不需要在使用ThreadLocal的值时,就应该手动调用remove方法清除该值。
(完)