当前位置:网站首页>Thread data sharing and security -threadlocal
Thread data sharing and security -threadlocal
2022-07-01 03:23:00 【Al_ tair】
ThreadLocal
Hello, everyone , I'm Xiao Sheng ! This part is my study ThreadLocal The notes
Thread data sharing and security - ThreadLocal
summary
ThreadLocal Is a class that will create separate copies of variables for each thread in multiple threads ; When using ThreadLocal To maintain variables , ThreadLocal A separate copy of the variable is created for each thread , Avoid data inconsistency caused by multi-threaded operation of shared variables
every last ThreadLocal object , Only one data can be associated for the current thread , If you want to associate multiple numbers for the current thread According to the , You need to use multiple ThreadLocal Object instances

Code example

public class Dog {
}
// ThreadLocalDemo
public class ThreadLocalDemo implements Runnable{
public static ThreadLocal<Object> threadLocal= new ThreadLocal<>();
public static void main(String[] args) {
// Create a process
new Thread(new ThreadLocalDemo()).start();
}
@Override
public void run() {
Dog dog = new Dog();
threadLocal.set(dog);
System.out.println("ThreadLocalDemo: " + Thread.currentThread().getName());
new Service().update();
}
}
// Service
public class Service {
public void update() {
Object o = ThreadLocalDemo.threadLocal.get();
System.out.println(o.getClass()); // threadLocal.Dog
String name = Thread.currentThread().getName();
System.out.println(" stay Service Of update() Threads name= " + name);
new Dao().update();
}
}
// Dao
public class Dao {
public void update() {
Object o = ThreadLocalDemo.threadLocal.get();
System.out.println(o.getClass()); // threadLocal.Dog
String name = Thread.currentThread().getName();
System.out.println(" stay Dao Of update() Thread is = " + name);
}
}
Source code analysis
// ThreadLocal.get() Method
public T get() {
// Get the current process t
Thread t = Thread.currentThread();
// From the container map Get the threadLocals
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
// ThreadLocal.set( data ) Method
public void set(T value) {
// Get the current process t
Thread t = Thread.currentThread();
// From the container map Get the threadLocals
ThreadLocalMap map = getMap(t);
/* ThreadLocalMap getMap(Thread t) { return t.threadLocals; } */
if (map != null) {
// If there is , Then overwrite the threadLocals The original data corresponding to the object ( It can be the object )
map.set(this, value);
} else {
// If it doesn't exist , Create t.threadLocals, Deposit in ThreadLocalMap object
// key:this( The method is called threadLocal object )value: Data to be stored
createMap(t, value);
/* void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); } */
}
}
边栏推荐
- Metadata in NFT
- Introduction and installation of Solr
- 【EXSI】主机间传输文件
- Leetcode 1818 absolute value, sorting, dichotomy, maximum value
- So easy deploy program to server
- Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
- 一文讲解发布者订阅者模式与观察者模式
- So easy 将程序部署到服务器
- Cloud native annual technology inventory is released! Ride the wind and waves at the right time
- Elk elegant management server log
猜你喜欢

【Qt】添加第三方库的知识补充

A few lines of transaction codes cost me 160000 yuan

Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example

几行事务代码,让我赔了16万

Nacos

Ridge regression and lasso regression

Chapter 03_ User and authority management

JUC learning

最好用的信任关系自动化脚本(shell)
![Servlet [first introduction]](/img/2a/aff3b93e43550d30a33c1683210d3a.png)
Servlet [first introduction]
随机推荐
So easy deploy program to server
Summary of problems encountered in debugging positioning and navigation
Kmeans
File upload and download
如何校验两个文件内容是否相同
第03章_用戶與權限管理
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
An article explaining the publisher subscriber model and the observer model
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
[machine learning] vectorized computing -- a must on the way of machine learning
监听器 Listener
过滤器 Filter
MySQL knowledge points
Go tool cli for command line implementation
leetcode 1818 绝对值,排序,二分法,最大值
雪崩问题以及sentinel的使用
Servlet [first introduction]
Promise中finally的用法
gcc使用、Makefile总结
CX5120控制汇川IS620N伺服报错E15解决方案