当前位置:网站首页>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
边栏推荐
- “如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
- . net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
- How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
- ThreadLocal 测试多线程变量实例
- 2022g2 boiler stoker examination question bank and online simulation examination
- The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
- Experiment 5 components and event handling
- Queue storage and circular queue
- Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
- Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
猜你喜欢

How does Microsoft solve the problem of multiple PC programs

MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】

Impact of AOT and single file release on program performance

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

Xamarin effect Chapter 21 expandable floating operation button in GIS
![[Mysql] LEFT函數 | RIGHT函數](/img/26/82e0f2280de011636c26931a74e749.png)
[Mysql] LEFT函數 | RIGHT函數

C read / write binary file
![[mock data] fastmock dynamically returns the mock content according to the incoming parameters](/img/1e/c4aad49d16fb21a19865ba75128e43.png)
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
![General test technology [II] test method](/img/b7/f661f446616ad6bfbbf48eb03ea82d.png)
General test technology [II] test method

C language to achieve address book - (static version)
随机推荐
C syntax pattern matching [switch expression]
Blazor University (11) component - replace attributes of subcomponents
MySQL installation pit
Five tips for cross-border e-commerce in 2022
Iotos IOT middle platform is connected to the access control system of isecure center
Advanced sorting - fast sorting
ASP. Net 6 middleware series - conditional Middleware
移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
幂等性实践操作,基于业务讲解幂等性
編碼電機PID調試(速度環|比特置環|跟隨)
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
[Mysql] LEFT函数 | RIGHT函数
The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
The most easy to understand dependency injection and control inversion
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
2022a special equipment related management (elevator) work license question bank and simulation examination
Fundamentals of software testing and development
[new version release] componentone added Net 6 and blazor platform control support
Xamarin effect Chapter 21 expandable floating operation button in GIS
Eight elder brothers chronicle [4]