相信很多人知道 ThreadLocal 是针对每个线程的,但是其中的原理相信大家不是很清楚,那咱们就一块看一下源码.
首先,我们先看看它的 set 方法.非常简单,从当前 Thread 中获取 map.那么这个 getMap 方法是什么样的呢?咱们继续看.
从当前线程中去获取单钱线程的 threadLocals. 继续跟进 t.threadLocals.
/**
* 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);
}
这个 threadLocals 定义在 Thread 里边,也就是每个线程里边维护者自己的 threadLocals,这样使得每个线程之间独立了.思路再次回头上边的 set 方法上,如果 map 不为 null, 那么重新进行 set 操作,如果为空,则需要创建这个 ThreadLocalMap.咱们看如何进行的创建.
/**
* 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;
}
以下为创建的过程,在 ThreadLocalMap 中有一个静态内部类,静态内部类和普通的类是一样的,静态属性和方法和静态内部类不一样,这一点大家要注意.首先根据当前的 ThreadLocal 的实例获取当前的 hashCode 然后再与 (初始容量 - 1)结果为 7,Entry 是 ThreadLocalMap 的静态内部类,类型为弱引用,也就是说,如果里边的 key 为 null 时,该 value 就是过期的 value, 后续将会被擦除.
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;
刚才是如果 map 为空的时候,重新创建了一个 ThreadLocalMap 赋值给了当前的 Thread, 如果当前 thread 的 map 不为空时,那么就需要获取当前的 threadLocalMap, 然后重新将值 set 上.
/**
* 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
*/
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
/**
* Construct a new map initially containing (firstKey, firstValue).
* ThreadLocalMaps are constructed lazily, so we only create
* one when we have at least one entry to put in it.
*/
ThreadLocalMap(ThreadLocal < ?>firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}
/**
* 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;
}
}
这块是它的 set 方法,那么咱们看一个下 get 方法,获取当前的 thread, 并且从 thread 中获取 threadLocalMap,然后取出其中的 value,如果没有找到这个 map 的话,初始化一个 key 为 ThreadLocal 的实例放入到当前的 thread 中的 ThreadLocalMap 中,并且将 value 设置为 null.最后将 null 返回给 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();
}
一般在 web 应用中 context 可以使用.比如 SimpleDateFormat 是非线程安全的,那么我们就可以借助这个类去进行处理,为了显示不同的格式.我自己写了一个例子如下:
使用这个 ThreadLocal 的时候,需要注意一些内容:
package com.hqs;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by huangqingshi on 2018/1/14.
*/
public class ThreadLocalExample {
//SimpleDateFormat 不是线程安全的
private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat();
}
};
public static void main(String[] args) {
SimpleDateFormat sdf = threadLocal.get();
sdf.applyPattern("yy/MM/dd");
System.out.println(sdf.format(new Date()));
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(new Date()));
}
}
18/01/14
01/14/2018
Process finished with exit code 0
如果定义一些大量的线程变量的,可能会出现不能创建线程的异常,因为一些 web server 针对线程会定义线程池,当使用 ThreadLocal 创建了很多线程,处理的时间比较久,那么这些线程不会释放,结果会导致栈溢出,程序没有响应.此时需要注意使用的时候,尽可能让当前线程处理完成.
还有可能会因此出现 Perm 溢出,比如 ThreadLocal 里边装入了一些静态的类,那么静态的类初始化的时候会在永久代,而且不会释放,那么创建对象的数量增多的话会导致线程出现这总异常,那么我们在这种情况下需要进行 remove 操作.如果 ThreadLocal 里边放入了一些基本类型话不会出现这种情况.
还有一种情况就是如果线程中的对象需要进行数据共享的话需要将 ThreadLocal 设置为静态变量.
好了,针对这块内容的分析和整理就这么多了,如果大家有什么问题的话,请告知~ 谢谢.
来源: https://www.cnblogs.com/huangqingshi/p/8284437.html