前言
本次源碼基於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着都是你自己獨有的,但是對於大部分人來說,銀行賬號已經夠用了,足夠安全。沒有必要使用自己的金庫來存錢了