当前位置:网站首页>ThreadLocal 测试多线程变量实例
ThreadLocal 测试多线程变量实例
2022-04-23 03:15:00 【JavaTestZhangy】
ThreadLocal,很多地方叫做线程本地变量,也有些地方叫做线程本地存储,其实意思差不多。可能很多朋友都知道ThreadLocal为变量在每个线程中都创建了一个副本,那么每个线程可以访问自己内部的副本变量。
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---再次查看主线程值------------");
System.out.println(test.getLong());
System.out.println(test.getString());
}
}
版权声明
本文为[JavaTestZhangy]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_23490959/article/details/84952610
边栏推荐
- 手机连接电脑后,QT的QDIR怎么读取手机文件路径
- 中后二叉建树
- Mysql database design specification
- TP5 email (2020-05-27)
- Aspnetcore configuration multi environment log4net configuration file
- A set of C interview questions about memory alignment. Many people make mistakes!
- C read / write binary file
- What kind of experience is it to prepare for a month to participate in ACM?
- 《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
- How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
猜你喜欢
Comprehensive calculation of employee information
可以接收多種數據類型參數——可變參數
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
C WPF UI framework mahapps switching theme
Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)
利用栈的回溯来解决“文件的最长绝对路径”问题
2022山东省安全员C证上岗证题库及在线模拟考试
C语言实现通讯录----(静态版本)
2022t elevator repair test simulation 100 questions and online simulation test
为什么BI对企业这么重要?
随机推荐
Ide-idea-problem
LoadRunner - performance testing tool
. net core current limiting control - aspnetcoreratelimit
Use of ADB command [1]
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
Comprehensive calculation of employee information
全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
利用栈的回溯来解决“文件的最长绝对路径”问题
【VS Code】解决jupyter文件在vs code中显示异常的问题
js递归树结构计算每个节点的叶子节点的数量并且输出
Huawei mobile ADB devices connection device is empty
Improvement of ref and struct in C 11
为什么BI对企业这么重要?
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
General testing technology [1] classification of testing
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
Xutils3 corrected a bug I reported. Happy
使用split来解决“最常见的单词”问题
可以接收多種數據類型參數——可變參數