Closed Draymonders closed 3 years ago
https://www.cnblogs.com/Draymonder/p/10433516.html
public T get() {
Thread t = Thread.currentThread();
// 获取线程的ThreadLocalMap
ThreadLocalMap map = getMap(t);
if (map != null) {
// 根据threadLocal去查找对应的值
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
// 如果没找到就尝试初始化
return setInitialValue();
}
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
map.set(this, value);
} else {
createMap(t, value);
}
}
先来一波容器和集合,方法按照增删查改列举出来
collection
List
朴素的方法
ArrayList
LinkedList
CopyOnWriteList
适合读多写少的场景
Map
HashMap
hash方法,根据hash方法可知,当key为null时,
hash(null)=0
, 固定插在tab[0]
put方法
get方法
concurrentHashMap
todo
Set
HashSet
todo
TreeSet
todo