当前位置:网站首页>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
边栏推荐
- This new feature of C 11, I would like to call it the strongest!
- 你真的懂hashCode和equals吗???
- Configuration table and page information automatically generate curd operation page
- 手机连接电脑后,QT的QDIR怎么读取手机文件路径
- Top 9 task management system in 2022
- Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
- Aspnetcore configuration multi environment log4net configuration file
- 一文了解全面静态代码分析
- Flink real-time data warehouse project - Design and implementation of DWS layer
- After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
猜你喜欢

类似Jira的十大项目管理软件

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list

可以接收多種數據類型參數——可變參數

Web Course Design - his system

JS inheritance

MySQL之explain关键字详解

PID debugging of coding motor (speed loop | position loop | follow)

TCP three handshakes and four waves

Configure automatic implementation of curd projects

OLED多级菜单记录
随机推荐
Source generator actual combat
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
The most easy to understand dependency injection and control inversion
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
[MySQL] left Function | Right Function
yes. Net future
[new version release] componentone added Net 6 and blazor platform control support
Comprehensive calculation of employee information
OLED多级菜单记录
Charles uses three ways to modify requests and responses
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
队列的存储和循环队列
[untitled]
OLED multi-level menu record
JS inheritance
Miniapi of. Net7 (special section): NET7 Preview3
编码电机PID调试(速度环|位置环|跟随)
The most easy to understand service container and scope of dependency injection
全新的ORM框架——BeetlSQL介绍