当前位置: 移动技术网 > IT编程>开发语言>Java > 从面试中的问题分析ThreadLocal

从面试中的问题分析ThreadLocal

2019年07月19日  | 移动技术网IT编程  | 我要评论

硅湖职业,肉包网,bmw m3 gts

threadlocal是什么

threadlocal是一个本地线程副本变量工具类。主要用于将私有线程和该线程存放的副本对象做一个映射,各个线程之间的变量互不干扰,在高并发场景下,可以实现无状态的调用,特别适用于各个线程依赖不通的变量值完成操作的场景。

从数据结构入手

下图为threadlocal的内部结构图

从上面的结构图,我们已经窥见threadlocal的核心机制:

  • 每个thread线程内部都有一个map。
  • map里面存储线程本地对象(key)和线程的变量副本(value)
  • 但是,thread内部的map是由threadlocal维护的,由threadlocal负责向map获取和设置线程的变量值。

所以对于不同的线程,每次获取副本值时,别的线程并不能获取到当前线程的副本值,形成了副本的隔离,互不干扰。

thread线程内部的map在类中描述如下:

public class thread implements runnable {
/* threadlocal values pertaining to this thread. this map is maintained
* by the threadlocal class. */
threadlocal.threadlocalmap threadlocals = null;
}

深入解析threadlocal

threadlocal类提供如下几个核心方法:

public t get()
public void set(t value)
public void remove()
  • get()方法用于获取当前线程的副本变量值。
  • set()方法用于保存当前线程的副本变量值。
  • initialvalue()为当前线程初始副本变量值。
  • remove()方法移除当前前程的副本变量值。

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)
return (t)e.value;
}
return setinitialvalue();
}
threadlocalmap getmap(thread t) {
return t.threadlocals;
}
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;
}
protected t initialvalue() {
return null;
}

步骤:

1.获取当前线程的threadlocalmap对象threadlocals

2.从map中获取线程存储的k-v entry节点。

3.从entry节点获取存储的value副本值返回。

4.map为空的话返回初始值null,即线程变量副本为null,在使用时需要注意判断nullpointerexception。

set()方法

/**
* 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);
}
threadlocalmap getmap(thread t) {
return t.threadlocals;
}
void createmap(thread t, t firstvalue) {
t.threadlocals = new threadlocalmap(this, firstvalue);
}

步骤:

1.获取当前线程的成员变量map

2.map非空,则重新将threadlocal和新的value副本放入到map中。

3.map空,则对线程的成员变量threadlocalmap进行初始化创建,并将threadlocal和value副本放入map中。

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
* <tt>initialvalue</tt> method in the current thread.
*
* @since 1.5
*/
public void remove() {
threadlocalmap m = getmap(thread.currentthread());
if (m != null)
m.remove(this);
}
threadlocalmap getmap(thread t) {
return t.threadlocals;
}

remove方法比较简单,不做赘述。

threadlocalmap

threadlocalmap是threadlocal的内部类,没有实现map接口,用独立的方式实现了map的功能,其内部的entry也独立实现。

在threadlocalmap中,也是用entry来保存k-v结构数据的。但是entry中key只能是threadlocal对象,这点被entry的构造方法已经限定死了。

static class entry extends weakreference<threadlocal> {
/** the value associated with this threadlocal. */
object value;
entry(threadlocal k, object v) {
super(k);
value = v;
}
}

entry继承自weakreference(弱引用,生命周期只能存活到下次gc前),但只有key是弱引用类型的,value并非弱引用。

threadlocalmap的成员变量:

static class threadlocalmap {
/**
* the initial capacity -- must be a power of two.
*/
private static final int initial_capacity = 16;
/**
* the table, resized as necessary.
* table.length must always be a power of two.
*/
private entry[] table;
/**
* the number of entries in the table.
*/
private int size = 0;

/**
* the next size value at which to resize.
*/
private int threshold; // default to 0
}

hash冲突怎么解决

和hashmap的最大的不同在于,threadlocalmap结构非常简单,没有next引用,也就是说threadlocalmap中解决hash冲突的方式并非链表的方式,而是采用线性探测的方式,所谓线性探测,就是根据初始key的hashcode值确定元素在table数组中的位置,如果发现这个位置上已经有其他key值的元素被占用,则利用固定的算法寻找一定步长的下个位置,依次判断,直至找到能够存放的位置。

threadlocalmap解决hash冲突的方式就是简单的步长加1或减1,寻找下一个相邻的位置。

/**
* increment i modulo len.
*/
private static int nextindex(int i, int len) {
return ((i + 1 < len) ? i + 1 : 0);
}
/**
* decrement i modulo len.
*/
private static int previndex(int i, int len) {
return ((i - 1 >= 0) ? i - 1 : len - 1);
}

显然threadlocalmap采用线性探测的方式解决hash冲突的效率很低,如果有大量不同的threadlocal对象放入map中时发送冲突,或者发生二次冲突,则效率很低。

所以这里引出的良好建议是:每个线程只存一个变量,这样的话所有的线程存放到map中的key都是相同的threadlocal,如果一个线程要保存多个变量,就需要创建多个threadlocal,多个threadlocal放入map中时会极大的增加hash冲突的可能。

threadlocalmap的问题

由于threadlocalmap的key是弱引用,而value是强引用。这就导致了一个问题,threadlocal在没有外部对象强引用时,发生gc时弱引用key会被回收,而value不会回收,如果创建threadlocal的线程一直持续运行,那么这个entry对象中的value就有可能一直得不到回收,发生内存泄露。

如何避免泄漏

既然key是弱引用,那么我们要做的事,就是在调用threadlocal的get()、set()方法时完成后再调用remove方法,将entry节点和map的引用关系移除,这样整个entry对象在gc roots分析后就变成不可达了,下次gc的时候就可以被回收。
如果使用threadlocal的set方法之后,没有显示的调用remove方法,就有可能发生内存泄露,所以养成良好的编程习惯十分重要,使用完threadlocal之后,记得调用remove方法。

threadlocal<session> threadlocal = new threadlocal<session>();
try {
threadlocal.set(new session(1, "misout的博客"));
// 其它业务逻辑
} finally {
threadlocal.remove();
}

应用场景

还记得hibernate的session获取场景吗?

private static final threadlocal<session> threadlocal = new threadlocal<session>();
//获取session
public static session getcurrentsession(){
session session = threadlocal.get();
//判断session是否为空,如果为空,将创建一个session,并设置到本地线程变量中
try {
if(session ==null&&!session.isopen()){
if(sessionfactory==null){
rbuildsessionfactory();// 创建hibernate的sessionfactory
}else{
session = sessionfactory.opensession();
}
}
threadlocal.set(session);
} catch (exception e) {
// todo: handle exception
}
return session;
}

为什么?每个线程访问数据库都应当是一个独立的session会话,如果多个线程共享同一个session会话,有可能其他线程关闭连接了,当前线程再执行提交时就会出现会话已关闭的异常,导致系统异常。此方式能避免线程争抢session,提高并发下的安全性。

使用threadlocal的典型场景正如上面的数据库连接管理,线程会话管理等场景,只适用于独立变量副本的情况,如果变量为全局共享的,则不适用在高并发下使用。

总结

  • 每个threadlocal只能保存一个变量副本,如果想要上线一个线程能够保存多个副本以上,就需要创建多个threadlocal。
  • threadlocal内部的threadlocalmap键为弱引用,会有内存泄漏的风险。
  • 适用于无状态,副本变量独立后不影响业务逻辑的高并发场景。如果如果业务逻辑强依赖于副本变量,则不适合用threadlocal解决,需要另寻解决方案。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网