当前位置:网站首页>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
边栏推荐
- 关于idea调试模式下启动特别慢的优化
- 12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
- Student achievement management
- Xutils3 corrected a bug I reported. Happy
- General testing technology [1] classification of testing
- Establishing and traversing binary tree
- . NETCORE sets the API post mode, which can accept parameters directly in parentheses
- 队列的存储和循环队列
- Impact of AOT and single file release on program performance
- Is it difficult to choose binary version control tools? After reading this article, you will find the answer
猜你喜欢

软件测试相关知识~

一套组合拳,打造一款 IDEA 护眼方案

TCP three handshakes and four waves

This new feature of C 11, I would like to call it the strongest!

Huawei mobile ADB devices connection device is empty

How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing

Fiddler use

2022A特种设备相关管理(电梯)上岗证题库及模拟考试

Xamarin effect Chapter 22 recording effect

Configuration table and page information automatically generate curd operation page
随机推荐
Impact of AOT and single file release on program performance
JSON related
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
It can receive multiple data type parameters - variable parameters
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
2022G2电站锅炉司炉考试题库及在线模拟考试
Source generator actual combat
C syntax pattern matching [switch expression]
Flink real-time data warehouse project - Design and implementation of DWS layer
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
ASP. Net 6 middleware series - conditional Middleware
Preview of converting doc and PDF to SWF file
[Mysql] LEFT函數 | RIGHT函數
socket編程 send()與 recv()函數詳解
你真的懂hashCode和equals吗???
MySQL grouping query rules
Blazor University (12) - component lifecycle
A set of combination boxing to create an idea eye protection scheme
Yes Redis using distributed cache in NE6 webapi
Queue storage and circular queue