文章目录
前言
请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i、
提示:以下是本篇文章正文内容,下面案例可供参考
什么是ThreadLocal变量
ThreadLoal 变量,线程局部变量,同一个 ThreadLocal 所包含的对象,在不同的 Thread 中有不同的副本。这里有几点需要注意:
- 因为每个 Thread 内有自己的实例副本,且该副本只能由当前 Thread 使用。这是也是 ThreadLocal 命名的由来。
- 既然每个 Thread 有自己的实例副本,且其它 Thread 不可访问,那就不存在多线程间共享的问题。
ThreadLocal 提供了线程本地的实例。它与普通变量的区别在于,每个使用该变量的线程都会初始化一个完全独立的实例副本。ThreadLocal 变量通常被private static修饰。当一个线程结束时,它所使用的所有 ThreadLocal 相对的实例副本都可被回收。
总的来说,ThreadLocal 适用于每个线程需要自己独立的实例且该实例需要在多个方法中被使用,也即变量在线程间隔离而在方法或类间共享的场景。
主要特点
- 线程隔离:
每个线程都拥有自己的变量副本,线程之间的变量副本互不影响,从而避免了多线程操作共享资源造成的数据不一致问题。 - 线程安全:
由于每个线程只能访问自己的变量副本,因此不需要额外的同步机制来保证线程安全。 - 减少参数传递:
在复杂的业务逻辑中,使用ThreadLocal可以避免在多个方法之间频繁传递参数,从而简化代码。
ThreadLocal实现原理
首先 ThreadLocal 是一个泛型类,保证可以接受任何类型的对象。
因为一个线程内可以存在多个 ThreadLocal 对象,所以其实是 ThreadLocal 内部维护了一个 Map ,这个 Map 不是直接使用的 HashMap ,而是 ThreadLocal 实现的一个叫做 ThreadLocalMap 的静态内部类。而我们使用的 get()、set() 方法其实都是调用了这个ThreadLocalMap类对应的 get()、set() 方法、set() 方法。
ThreadLocal本身不存放数据,而是使用线程(Thread)中的threadLocals属性来存储每个线程的变量副本。threadLocals属性对应的是一个ThreadLocalMap的静态内部类对象,该对象内部维护了一个Entry数组来存储键值对(其中键是ThreadLocal的引用,值是线程变量的副本)。
- 当调用 ThreadLocal的set() 方法时,会将自身的引用(this)作为键,用户传入的值作为值存入ThreadLocalMap中。
- 当调用ThreadLocal的get() 方法时,会返回当前线程ThreadLocalMap中以当前ThreadLocal对象为键的值。
- 如果当前线程没有对应的ThreadLocalMap,则会创建一个新的ThreadLocalMap,并将当前ThreadLocal对象的引用作为键,用户指定的初始值(如果没有指定则默认为null)作为值存入其中。
set 方法: 设置当前线程的变量副本值为value。
/**
* 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方法: 获取当前线程所对应的变量副本的值,如果此线程从未设置过值,则返回null或者初始值(如果有的话)。
/**
* 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();
}
remove(): 删除当前线程保存的变量副本,如果不主动清理,可能会造成内存泄露。
/**
* Removes the current thread's value for this thread-local
* variable. If this thread-local variable is subsequently
* {@linkplain #get read} by the current thread, its value will be
* reinitialized by invoking its {@link #initialValue} method,
* unless its value is {@linkplain #set set} by the current thread
* in the interim. This may result in multiple invocations of the
* {@code initialValue} method in the current thread.
*
* @since 1.5
*/
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null) {
m.remove(this);
}
}
createMap方法:
/**
* 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);
}
最终的变量是放在了当前线程的 ThreadLocalMap 中,并不是存在 ThreadLocal 上,ThreadLocal 可以理解为只是ThreadLocalMap的封装,传递了变量值。
ThreadLocal 使用场景
线程上下文信息传递:
在web应用中,服务器接收到请求后,需要在不同的过滤器、处理器链路中传递用户会话信息,此时可以将这些信息存放在ThreadLocal中。
避免同步开销:
对于那些只需要在单个线程内保持状态,不需要线程间共享的数据,使用ThreadLocal可以避免使用锁带来的性能损耗。
数据库连接、事务管理:
在多线程环境下,每个线程可以有自己的数据库连接,可以使用ThreadLocal存储当前线程的数据库连接对象,以确保线程安全。
如上文所述,ThreadLocal 适用于如下两种场景
- 每个线程需要有自己单独的实例
- 实例需要在多个方法中共享,但不希望被多线程共享
对于第一点,每个线程拥有自己实例,实现它的方式很多。例如可以在线程内部构建一个单独的实例。ThreadLoca 可以以非常方便的形式满足该需求。
对于第二点,可以在满足第一点(每个线程有自己的实例)的条件下,通过方法间引用传递的形式实现。ThreadLocal 使得代码耦合度更低,且实现更优雅。
1. 存储用户Session
一个简单的用ThreadLocal来存储Session的例子:
private static final ThreadLocal threadSession = new ThreadLocal();
public static Session getSession() throws InfrastructureException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
s = getSessionFactory().openSession();
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
return s;
}
2. 解决线程安全的问题
比如Java7中的SimpleDateFormat不是线程安全的,可以用ThreadLocal来解决这个问题:
public class DateUtil {
private static ThreadLocal<SimpleDateFormat> format1 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public static String formatDate(Date date) {
return format1.get().format(date);
}
}
这里的DateUtil.formatDate()就是线程安全的了。(Java8里的 java.time.format.DateTimeFormatter是线程安全的,Joda time里的DateTimeFormat也是线程安全的)。
3. ThreadLocalRandom
ThreadLocalRandom使用ThreadLocal的原理,让每个线程内持有一个本地的种子变量,该种子变量只有在使用随机数时候才会被初始化,多线程下计算新种子时候是根据自己线程内维护的种子变量进行更新,从而避免了竞争。
用法:
ThreadLocalRandom.current().nextInt(100)
注意事项
- 内存泄露:
当线程结束生命周期后,如果没有显式调用remove()方法,存储在线程本地变量表中的ThreadLocal变量副本不会自动删除,这可能导致它们无法被垃圾回收,尤其是在线程池场景中,如果线程会被复用,这个问题更为突出。 - 线程安全的误解:
虽然ThreadLocal保证了每个线程只能访问自己的变量副本,但是它并不能保证变量副本本身的线程安全性。即如果存放在ThreadLocal中的对象不是线程安全的,多个线程通过各自的ThreadLocal访问相同的非线程安全对象时,还需要采取额外的同步措施。 - 过度使用:
不恰当的使用ThreadLocal可能导致代码逻辑变得复杂,增加维护难度,尤其是当线程间本来就需要共享数据时,不应该滥用ThreadLocal避免数据交换。 - 跨线程错误访问:
在多线程环境中,如果不明确线程间的数据隔离,误以为ThreadLocal存储的数据在整个应用范围内可见,就会导致逻辑错误。
内存泄漏问题
实际上 ThreadLocalMap 中使用的 key 为 ThreadLocal 的弱引用,弱引用的特点是,如果这个对象只存在弱引用,那么在下一次垃圾回收的时候必然会被清理掉。
所以如果 ThreadLocal 没有被外部强引用的情况下,在垃圾回收的时候会被清理掉的,这样一来 ThreadLocalMap中使用这个 ThreadLocal 的 key 也会被清理掉。但是,value 是强引用,不会被清理,这样一来就会出现 key 为 null 的 value。
ThreadLocalMap实现中已经考虑了这种情况,在调用 set()、get()、remove() 方法的时候,会清理掉 key 为 null 的记录。如果说会出现内存泄漏
,那只有在出现了 key 为 null 的记录后,没有手动调用 remove() 方法,并且之后也不再调用 get()、set()、remove() 方法的情况下。
建议回收自定义的ThreadLocal变量,尤其在线程池场景下,线程经常会被复用,如果不清理自定义的 ThreadLocal变量,可能会影响后续业务逻辑和造成内存泄露等问题。 尽量在代理中使用try-finally块进行回收:
objectThreadLocal.set(userInfo);
try {
// ...
}
finally {
objectThreadLocal.remove();
}
综上所述,ThreadLocal是Java中一个非常有用的工具类,它可以在多线程环境中提供线程级别的数据隔离和存储。然而,在使用时需要注意其可能带来的内存泄露和线程安全等问题,并谨慎选择使用场景。
总结
我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!
参考链接、