当前位置:网站首页>Examples of ThreadLocal usage scenarios under multithreading
Examples of ThreadLocal usage scenarios under multithreading
2022-07-26 12:28:00 【User 9919783】
In business development ,threadLocal There are two business scenarios that are used more .
1、threadlocal Ensure that each thread has its own object , Create a copy of the local thread , Ensure that each thread has its own copy , This ensures thread safety .
2、threadlocal It can act like a global variable , In one of the methods, use set In the data , Other methods can be used get Direct access to , Avoid passing parameters , The function is to save information independently for each thread .
Scene one :
result : Three threads are opened , Because in the threadlocal, Each thread has its own local copy , It won't add to 15, It is n Maximum output 5.
And that is simpledateformat It is also applicable to scenario 1 , When thread pool is on , Submit 10000 When there are more tasks , Each thread creates its own simpledateformat It's going to cost a lot , And it takes up memory , Destroying objects is also costly : In this case , Our first thought is that all threads share one simpledataformate, But it's not thread safe , How to solve it , Add... To him synchrolized lock , This will solve the problem , But when the thread is running , There will be a blockage , It affects efficiency very much , At the moment threadlocal Is a very good solution .
threadlocal Will make every thread simpledateformat Independent , If the thread pool Executors.newFixedThreadPool(15) Turn on 15 Threads , It will only create 15 individual simpledateformat, And thread safe , It won't affect each other before .
Scene two :
Scenario 2 is similar to global variables , It avoids the trouble of transferring parameters , Ensure that the object values obtained by the method are the same , First store the object in localthread Inside , And then get , Finally, remember remove fall .
ask : Used in thread pool threadlocal Why is it easy to cause memory leakage ?
Because the thread lifetime in the thread pool is too long , It is often destroyed after the program stops , such threadlocal Held threadlocalmap Will never be recycled , terms of settlement , Can be manually in finally Block calls its remove() Method .
边栏推荐
- 10. 509. Introduction to PKCs file format
- 需不需要上线MES系统,你在纠结什么?
- El form displays two columns per row, with the bottom button centered
- Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
- 三维点云课程(八)——特征点匹配
- Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
- Real time synchronization and conversion of massive data based on Flink CDC
- DS-24C/DC220V时间继电器
- Use the jsonobject object in fastjason to simplify post request parameter passing
- Y9000p2022 reinstallation win10 problem
猜你喜欢

论文阅读-MLPD:Multi-Label Pedestrian Detector in Multispectral Domain(海康威视研究院实习项目)

HTAP comes at a price

11 "pocket" universities in China! Running on campus and leaving the school before accelerating

How do children's playgrounds operate?

STM32 drives hc05 Bluetooth serial port communication module

Here blog: running a large language model in a production environment - overview of the reasoning framework

UE5 官方案例Lyra 全特性详解 7.资源管理

全国职业院校技能大赛网络安全B模块wirshark数据包分析 wireshark0051.pcap

海外APP推送(下篇):海外厂商通道集成指南

QT入门引导 及其 案例讲解
随机推荐
Oracle AWR report script: SQL ordered by elapsed time
Flink's real-time data analysis practice in iFLYTEK AI marketing business
Flutter's learning path
回溯——491. 递增子序列
Ssj-21b time relay
Tencent cloud and smart industry business group (CSIG) adjusted the organizational structure and established the digital twin product department
Some common writing methods and skills
Access数据库无法连接
空洞卷积详解(输入输出大小分析)
Backtracking - 46. Full arrangement
什么是回调函数,对于“回”字的理解
Pytest interface automation test framework | setup and teardown functions of pytest
Real time synchronization and conversion of massive data based on Flink CDC
Beauty salon management system unified management system?
Codepoint 58880 not found in font, aborting. Flutter build APK reports an error
.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI
MATLAB中strjoin函数使用
Sim900a based on STM32 sends short messages in Chinese and English
腾讯云与智慧产业事业群(CSIG)调整组织架构,成立数字孪生产品部
“2022华为开发者大赛中国区东部赛区开幕式”在福州成功举办