当前位置:网站首页>ThreadLocal test multithreaded variable instance
ThreadLocal test multithreaded variable instance
2022-04-23 03:18:00 【JavaTestZhangy】
ThreadLocal, Many places are called thread local variables , Some places are called thread local storage , In fact, the meaning is similar . Maybe a lot of friends know ThreadLocal A copy of the variable is created in each thread , Then each thread can access its own internal copy variables .
public class ThreadLocalTest {
ThreadLocal<Long> longLocal = new ThreadLocal<Long>();
ThreadLocal<String> stringLocal = new ThreadLocal<String>();
public void set() {
longLocal.set(Thread.currentThread().getId());
stringLocal.set(Thread.currentThread().getName());
}
public long getLong() {
return longLocal.get();
}
public String getString() {
return stringLocal.get();
}
public static void main(String[] args) throws InterruptedException {
final ThreadLocalTest test = new ThreadLocalTest();
test.set();
System.out.println("----------main---------------");
System.out.println(test.getLong());
System.out.println(test.getString());
Thread thread1 = new Thread(){
public void run() {
test.set();
System.out.println("----------thread1---------------");
System.out.println(test.getLong());
System.out.println(test.getString());
System.out.println("-------------------------");
};
};
thread1.start();
thread1.join();
Thread thread2 = new Thread(){
public void run() {
test.set();
System.out.println("---------thread2----------------");
System.out.println(test.getLong());
System.out.println(test.getString());
System.out.println("-------------------------");
};
};
thread2.start();
thread2.join();
System.out.println("----------main--- Check the main route value again ------------");
System.out.println(test.getLong());
System.out.println(test.getString());
}
}
版权声明
本文为[JavaTestZhangy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230314477609.html
边栏推荐
- 2022A特种设备相关管理(电梯)上岗证题库及模拟考试
- Student achievement management
- Drawing polygons with < polygon / > circular array in SVG tag
- 【VS Code】解决jupyter文件在vs code中显示异常的问题
- Web Course Design - his system
- Mysql database design specification
- EasyUI's combobox implements three-level query
- Iotos IOT middle platform is connected to the access control system of isecure center
- Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
- General testing technology [1] classification of testing
猜你喜欢
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
在.NE6 WebApi中使用分布式缓存Redis
ASP. Net 6 middleware series - conditional Middleware
Blazor University (11) component - replace attributes of subcomponents
2022a special equipment related management (elevator) work license question bank and simulation examination
Iotos IOT middle platform is connected to the access control system of isecure center
2022年度Top9的任务管理系统
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
随机推荐
Advanced sorting - fast sorting
Charles uses three ways to modify requests and responses
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
xutils3修改了我提报的一个bug,开心
【无标题】
MySQL keyword group_ Concat, combined connection query
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
编码电机PID调试(速度环|位置环|跟随)
OLED multi-level menu record
Cefsharp stores cookies and reads cookies
Course design of Database Principle -- material distribution management system
搭建XAMPP时mysql端口被占用
EasyUI's combobox implements three-level query
Five tips for cross-border e-commerce in 2022
A set of combination boxing to create an idea eye protection scheme
月薪10k-20k都无法回答的事务问题,你会吗?
The most understandable life cycle of dependency injection
Use of ADB command [1]
Mysql database, inconsistent index character set, slow SQL query, interface timeout
OLED多级菜单记录