带你读《2022技术人的百宝黑皮书》——合理使用线程池以及线程变量(10)

简介: 带你读《2022技术人的百宝黑皮书》——合理使用线程池以及线程变量(10)

带你读《2022技术人的百宝黑皮书》——合理使用线程池以及线程变量(9)https://developer.aliyun.com/article/1340060?groupCode=taobaotech


ThreadLocal原理

 

Thread 内部维护了一个 ThreadLocal.ThreadLocalMap 实例(threadLocals),ThreadLocal 的操作都是围绕threadLocals 来操作的。

 

image.pngthreadLocal.get()方法

 

 

 

1

/**

 

2

 

* Returns the value in the current thread's copy of this

3

 

* thread-local variable. If the variable has no value for the

4

 

* current thread, it is first initialized to the value returned

5

 

* by an invocation of the {@link #initialValue} method.

6

 

*

7

 

* @return the current thread's value of this thread-local

8

 

*/

 

public T get() {
// 1. 获取当前线程
Thread t = Thread.currentThread();
// 2. 获取当前线程内部的ThreadLocalMap变量t.threadLocals;
ThreadLocalMap map = getMap(t);
// 3. 判断map是否为null
if (map != null) {
// 4. 使用当前threadLocal变量获取entry
ThreadLocalMap.Entry e = map.getEntry(this);
// 5. 判断entry是否为null
if (e != null) {
// 6.返回Entry.value
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
  }
  }
// 7. 如果map/entry为null设置初始值
return 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() {
// 1. 初始化value,如果重写就用重写后的value,默认null
T value = initialValue();
// 2. 获取当前线程
Thread t = Thread.currentThread();
// 3. 获取当前线程内部的ThreadLocalMap变量
ThreadLocalMap map = getMap(t);
if (map != null)
// 4. 不为null就set, key: threadLocal, value: value
map.set(this, value);
else
// 5. map若为null则创建ThreadLocalMap对象
createMap(t, value);
return value;
}
/**
*Create the map associated with a ThreadLocal. Overridden in
*InheritableThreadLocal.
*
*@param t the current thread
*@param firstValue value for the initial entry of the map
*/
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
/**
*Construct a new map initially containing (firstKey, firstValue).
*ThreadLocalMaps are constructed lazily, so we only create
*one when we have at least one entry to put in it.
*/
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
// 1. 初始化entry数组,size: 16
table = new Entry[INITIAL_CAPACITY];
// 2. 计算value的index
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
// 3. 在对应index位置赋值
table[i] = new Entry(firstKey, firstValue);
// 4. entry size size = 1;
// 5. 设置threshold: threshold = len * 2 / 3; setThreshold(INITIAL_CAPACITY);
}
/**
*Set the resize threshold to maintain at worst a 2/3 load factor.
*/
private void setThreshold(int len) { threshold = len * 2 / 3;
}

带你读《2022技术人的百宝黑皮书》——合理使用线程池以及线程变量(11)https://developer.aliyun.com/article/1340058?groupCode=taobaotech

相关文章
|
2天前
|
存储 安全 Java
Java中的线程安全与同步技术
Java中的线程安全与同步技术
|
1天前
|
数据采集 Java Unix
10-多线程、多进程和线程池编程(2)
10-多线程、多进程和线程池编程
|
1天前
|
安全 Java 调度
10-多线程、多进程和线程池编程(1)
10-多线程、多进程和线程池编程
|
9天前
|
Java
【技术瑜伽师】Java 线程:修炼生命周期的平衡之道,达到多线程编程的最高境界!
【6月更文挑战第19天】Java多线程编程犹如瑜伽修行,从创建线程开始,如`new Thread(Runnable)`,到启动线程的活跃,用`start()`赋予生命。面对竞争与冲突,借助同步机制保证资源访问的有序,如`synchronized`关键字。线程可能阻塞等待,如同瑜伽的静止与耐心。完成任务后线程终止,整个过程需密切关注状态变换,以求多线程间的和谐与平衡。持续修炼,如同瑜伽般持之以恒,实现高效稳定的多线程程序。
|
9天前
|
Java 开发者
【技术成长日记】Java 线程的自我修养:从新手到大师的生命周期修炼手册!
【6月更文挑战第19天】Java线程之旅,从新手到大师的进阶之路:始于创建线程的懵懂,理解就绪与运行状态的成长,克服同步难题的进阶,至洞悉生命周期的精通。通过实例,展示线程的创建、运行与同步,展现技能的不断提升与升华。
|
9天前
|
Java
【技术解码】Java线程的五味人生:新建、就绪、运行、阻塞与死亡的哲学解读!
【6月更文挑战第19天】Java线程生命周期如同人生旅程,经历新建、就绪、运行、阻塞至死亡五阶段。从`new Thread()`的诞生到`start()`的蓄势待发,再到`run()`的全力以赴,线程在代码中奔跑。阻塞时面临挑战,等待资源释放,最终通过`join()`或中断结束生命。线程的每个状态转变,都是编程世界与哲思的交汇点。
|
1天前
|
SQL 安全 Java
JUC多线程-线程池-Thredalocal-CAS-AQS-死锁
JUC多线程-线程池-Thredalocal-CAS-AQS-死锁
|
24天前
|
开发框架 监控 Java
【.NET Core】多线程之线程池(ThreadPool)详解(二)
【.NET Core】多线程之线程池(ThreadPool)详解(二)
34 3
|
24天前
|
SQL 开发框架 Java
【.NET Core】多线程之线程池(ThreadPool)详解(一)
【.NET Core】多线程之线程池(ThreadPool)详解(一)
25 2
|
2天前
|
Java
java线程之线程池
java线程之线程池
8 0

热门文章

最新文章