当前位置:网站首页>Principle analysis of ThreadLocal source code
Principle analysis of ThreadLocal source code
2022-06-25 15:59:00 【Favorite grapes】
What( What is? ThreadLocal)
Commonly known as thread local variables , Is a method that can be used in the current thread , Just get The value set by this thread Tools for .
Like a string ,A The thread is set “abc”,B The thread is set “def”, When you want two threads to read again , Namely ,A Read “abc”,B Read yes “def”.
Why( Why use it ?)
Want different threads , Use the same variable , But the values read through it are the values set by each .
How( How to use it? ?)
ThreadLocal<String> tl = new ThreadLocal<>();
// thread1
tl.set("abc");
sout(tl.get()); // abc
// thread2
tl.set("def");
sout(tl.get()); // def
ThreadLocal How to do it ?
1、 Simply create one ThreadLocal Objects are useless . Only called set() perhaps get() When the method is used ,ThreadLocal It's just starting to work .
2. from set Speaking of , When we call set When the method is used ,ThreadLocal Internal will get the current Thread One inside threadLocals object , And its essence is ThreadLocalMap( A custom HashMap, There's a... In it Entry Array ,Entry Inherited from WeakReference,Entry Added a value attribute , Used to store values )
3. ThreadLocal<?> As Entry Value of weak reference , Is equivalent to HashMap Of Key.
4. So when you call get perhaps set When the method is used , It will go to the current thread threadlocals Read or set the corresponding value in .
ThreadLocalMap And traditional HashMap The difference between ?
It has only one array inside , When hash collision occurs , Will get the next array index , Instead of using the zipper method to create a linked list from the back . So his architecture , It's more like an array , Only the index value is through hash The algorithm determines .
A less elegant drawing illustrates :

边栏推荐
- 说下你对方法区演变过程和内部结构的理解
- Mt60b1g16hc-48b:a micron memory particles FBGA code d8bnk[easy to understand]
- Go development team technical leader Russ Cox sends a document to share go's version control history
- 镁光256Gb NAND Flash芯片介绍
- 面试官:你简历上说精通mysql,那你说下聚簇/联合/覆盖索引、回表、索引下推
- Mapbox map - inconsistent coordinate system when docking GIS layers?
- Desktop development (Tauri) opens the first chapter
- TensorFlow加载cifar10数据集
- Traversal and branch judgment of JS (case on June 24, 2022)
- Leetcode topic [array]-34- find the first and last positions of elements in a sorted array
猜你喜欢
随机推荐
Mixed density network (MDN) for multiple regression explanation and code example
将一个文件写入到另一个文件的标记位置
LeCun预言AGI:大模型和强化学习都是斜道!我的「世界模型」才是新路
剑指 Offer 09. 用两个栈实现队列
Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
什么是NFT数字藏品?
中国高校首次!全球唯一!同济学子斩获国际大奖
Super comprehensive custom deep copy function
Desktop development (Tauri) opens the first chapter
一行代码可以做什么?
Traversal and branch judgment of JS (case on June 24, 2022)
SQL最常用的语句
剑指 Offer 07. 重建二叉树
JS add custom attributes to elements
Download and installation tutorial of consumer
Differences between = = and = = = in JS (detailed explanation)
Introduction to database transactions
Write one file to the marked location of another file
报错:homebrew-core is a shallow clone
Client development (electron) system level API usage







