Zhou Study hard, improve every day.

ThreadLocal源码阅读

2020-05-09
本文 411 字,阅读全文约需 2 分钟

1.ThreadLocal源码

1.1 简介

1.2 set方法

//thread的一个成员变量
ThreadLocal.ThreadLocalMap threadLocals = null;

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.3 get方法


下一篇 远程执行命令

Comments

Content