对ThreadLocal的认识

ThreadLocal的初步认识

在正式解析ThreadLocal之前,我们先来看 一个例子:

public class ThreadLocalTest {
    //创建并初始化一个ThreadLocal变量
    private static ThreadLocal<Integer> cnt = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public static void main(String[] args){
        System.out.println(Thread.currentThread().getName() + " before write, get cnt " + cnt.get());
        cnt.set(1);
        //创建线程一对ThreadLocal变量进行修改和读取
        new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " before write, get cnt : " + cnt.get());
                cnt.set(2);
                System.out.println(Thread.currentThread().getName() + " after write, get cnt : " + cnt.get());
            }
        }.start();
        //创建线程二对ThreadLocal变量进行修改和读取
        new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " before write, get cnt : " + cnt.get());
                cnt.set(3);
                System.out.println(Thread.currentThread().getName() + " get cnt : " + cnt.get());

            }
        }.start();
        //主线程进行读取
        System.out.println(Thread.currentThread().getName() + " after write, get cnt : " + cnt.get());
    }

}
输出结果:
main before write, get cnt 0
Thread-0 before write, get cnt : 0
Thread-0 after write, get cnt : 2
main after write, get cnt : 1
Thread-1 before write, get cnt : 0
Thread-1 get cnt : 3

程序中存在三个线程(包括主线程)对变量cnt进行修改并读取,根据结果,我们发现不同线程之间对变量的修改和读取都是独立的,互不影响的。实际上,这就是ThreadLocal的特色。

ThreadLocal在每个线程中对变量会创建一个副本,即每个线程内部都会有一个变量,且在线程内部任何地方可以使用,线程之间互不影响。

ThreadLocal的深入理解

ThreadLocal类主要提供以下几个方法:

public T get() {}
public void set(T value) {}
public void remove() {}
protected T initValue() {}
  • 我们先看到initValue方法:
protected T initialValue() {
     return 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();
}

首先通过Thread.currentThread()方法获取到当前线程,然后再根据当前线程调用getMap方法获取到ThreadLocalMap对象。那么我们来看看getMap方法:

ThreadLocalMap getMap(Thread t) {
    return t.threadLocals;
}

我们可以看到它返回的是线程中的一个threadLocals变量,而我们在Thread类中,也可以发现这个变量:

/* ThreadLocal values pertaining to this thread. This map is maintained
 * by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;

若map不为空,那么将通过map.getEntry(this)方法来获取到Entry对象,我们来看看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对象是ThreadLocal的一个弱引用而已,这里应该是方便GC的回收,防止内存溢出。我们继续看到getEntry方法:

private Entry getEntry(ThreadLocal<?> key) {
    int i = key.threadLocalHashCode & (table.length - 1);
    Entry e = table[i];
    if (e != null && e.get() == key)
        return e;
    else
        return getEntryAfterMiss(key, i, e);
}

看到这里,可能就有些懵逼了,这里的threadLocalHashCodetable完全不知所云。这里,我们就要回头看ThreadLocal中的一些变量:

private final int threadLocalHashCode = nextHashCode();

/**
  * The next hash code to be given out. Updated atomically. Starts at
  * zero.
  */
private static AtomicInteger nextHashCode =
    new AtomicInteger();

/**
  *The difference between successively generated hash codes - turns
  * implicit sequential thread-local IDs into near-optimally spread
  * multiplicative hash values for power-of-two-sized tables.
  */
private static final int HASH_INCREMENT = 0x61c88647;

/**
 * Returns the next hash code.
 */
private static int nextHashCode() {
    return nextHashCode.getAndAdd(HASH_INCREMENT);
}

在这里定义了一个类型为AtomicInteger类型的变量,即每次创建一个ThreadLocal对象,都会得到它所对应的值,因为它是自增的,然后threadLocalHashCode变量则为该值得一个hash值。而table变量则是声明在ThreadLocalMap中:

/**
 * The table, resized as necessary.
 * table.length MUST always be a power of two.
 */
private Entry[] table;

它的作用为存储值,其中threadLocalHashCode经过处理后便为ThreadLocal所对应的索引。
获取到Entry对象后,我们就可以通过Entry对象获取到所对应的值,返回即可。

若map为空,则调用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;
}

方法中,获取到起初始值,并将当前线程与value进行绑定,存入map对象中,以便下一次的调用,若map为空,则创建map对象。

void createMap(Thread t, T firstValue) {
    t.threadLocals = new ThreadLocalMap(this, firstValue);
}

我们可以看到就是返回一个ThreadLocalMap对象。

  • 接下来我们看到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);
}

这里的思路很简单,就是先获取到map对象,若map不为空,则更新其值,否则创建一个map对象。

  • 最后我们看到remove方法:
public void remove() {
   ThreadLocalMap m = getMap(Thread.currentThread());
   if (m != null)
       m.remove(this);
}

即获取到map对象后,将map中的值移除。

注意:在使用时,若没有重写initialValue方法,那么在调用get方法前,必须调用set方法,否则会报空指针异常。

TheadLocal的应用

通常当我们想要共享一个变量,但该变量又不是线程安全的,并且也不想通过加锁机制来降低并发性,那么就可以用ThreadLocal来维护一个线程一个实例。

  • 应用一:数据库连接
public class ConnectionManager {
    private static ThreadLocal<Connection> connectionHolder = 
            new ThreadLocal<Connection>(){
                @Override
                protected Connection initialValue() {
                    try {
                        return DriverManager.getConnection("localhost","username","password");
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            };
    
    public static Connection getConnection(){
        return connectionHolder.get();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

  • Android Handler机制系列文章整体内容如下: Android Handler机制1之ThreadAnd...
    隔壁老李头阅读 7,486评论 4 30
  • 前言 ThreadLocal很多同学都搞不懂是什么东西,可以用来干嘛。但面试时却又经常问到,所以这次我和大家一起学...
    liangzzz阅读 12,333评论 14 228
  • 原创文章&经验总结&从校招到A厂一路阳光一路沧桑 详情请戳www.codercc.com 1. ThreadLoc...
    你听___阅读 6,635评论 8 19
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,014评论 11 349
  • 章部:您好! 连锁直营店环保影响情况如下: 1、绍兴店:绍兴市卓烨机电有限公司,已经放假,开工时间暂时还没有确定。...
    松松劲柏阅读 121评论 0 0