前言
本次源码基于JDK1.8版本
源码分析
get方法
public T get() {
//获得对应的线程
Thread t = Thread.currentThread();
//通过线程获得对应的map
ThreadLocalMap map = getMap(t);
//判断map是否为空,不为空则拿到对应线程作为Key的value
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
//map为空,返回初始值
return setInitialValue();
}
setInitialValue方法
private T setInitialValue() {
//获得一个null
T value = initialValue();
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
//如果map不为空,设置null,否则,创建map
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
}
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
set方法
public void set(T value) {
//和get类型,获得map然后进行map的set方法
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
remove方法
//获得map中,如果不为空,则移除
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}
ThreadLocal最主要的部分在于他的内部类ThreadLocalMap,这个里面放了一个map集合,key就是我们的线程,value就是线程需要存放的变量
举例使用
public class Test15 extends Thread{
private ThreadLocal<String> threadLocal = new ThreadLocal<String>();
private ThreadLocal<String> threadLocal2 = new ThreadLocal<String>();
public Test15(){
}
public Test15(ThreadLocal<String> threadLocal) {
this.threadLocal = threadLocal;
}
public Test15(ThreadLocal<String> threadLocal,ThreadLocal<String> threadLocal2) {
this.threadLocal = threadLocal;
this.threadLocal2 = threadLocal2;
}
public void set(String s){
threadLocal.set(s);
}
public void get(){
String name = threadLocal.get();
System.out.println("线程"+Thread.currentThread().getName()+"获得"+name);
}
public void set2(String s){
threadLocal2.set(s);
}
public void get2(){
String name = threadLocal2.get();
System.out.println("线程"+Thread.currentThread().getName()+"获得"+name);
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
set(i+"");
get();
}
for (int i = 100; i < 110; i++) {
set2(i+"");
get2();
}
}
public static void main(String[] args) {
ThreadLocal<String> threadLocal = new ThreadLocal<String>();
ThreadLocal<String> threadLocal2 = new ThreadLocal<String>();
Test15 test1 = new Test15(threadLocal,threadLocal2);
test1.setName("test1");
Test15 test2 = new Test15(threadLocal,threadLocal2);
test2.setName("test2");
test1.start();
test2.start();
}
}
可以看到每一个线程有自己的单独的变量可以进行控制,对比局部变量的优点是相对于局部变量,可以占用更少的类的属性相关的。也可以用于所有类的初始赋值处理等通用方法。
简单来说:threalLocal就像一个银行(map),每个人(就是线程key)都有自己的账号存放在上面管理钱财(value),但是局部变量就像自己的金库,只放你自己的钱。虽然2着都是你自己独有的,但是对于大部分人来说,银行账号已经够用了,足够安全。没有必要使用自己的金库来存钱了