当前位置:网站首页>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); } */
}
}
边栏推荐
- Introduction to EtherCAT
- 【日常训练】1175. 质数排列
- Introduction and basic knowledge of machine learning
- 8 pits of redis distributed lock
- Latest interface automation interview questions
- HTB-Lame
- [applet project development -- Jingdong Mall] classified navigation area of uni app
- Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
- 第03章_用户与权限管理
- Detailed explanation of ES6 deconstruction grammar
猜你喜欢

终极套娃 2.0 | 云原生交付的封装

HTB-Lame

Data exchange JSON

Communication protocol -- Classification and characteristics Introduction
![Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]](/img/83/e3c9d8eda9d5351d4c54928d3b090b.png)
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]

Hello World generation

Druid监控统计数据源

Keil5中如何做到 0 Error(s), 0 Warning(s).

Introduction and installation of Solr

Ridge regression and lasso regression
随机推荐
伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
IEDA 右键源码文件菜单简介
Avalanche problem and the use of sentinel
[us match preparation] complete introduction to word editing formula
Metadata in NFT
Nacos
VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
JS to find duplicate elements in two arrays
Go tool cli for command line implementation
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
Analyze datahub, a new generation metadata platform of 4.7K star
Error accessing URL 404
XXL job User Guide
Hello World generation
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
实战 ELK 优雅管理服务器日志
ES6解构语法详解
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
gcc使用、Makefile总结
Subnet division and subnet summary