当前位置:网站首页>Basic principle of synchronized lock
Basic principle of synchronized lock
2022-07-23 21:20:00 【Swarford】
Java Memory model review :

- Java All variables are stored in main memory
- Each thread has its own working memory , It stores the copy of variables used by the thread ( This copy is a copy of this variable in main memory )
- All operations of a thread on shared variables must be in its own The working memory In the middle of , Can't read and write directly in main memory
- Variables in the working memory of other threads cannot be accessed directly between different threads , The transfer of variable values between threads needs to be completed through main memory .
Threads 1 Changes to shared variables , To be threaded 2 See in time , It has to go through the following 2 A process :
① Put the working memory 1 The shared variables updated in are refreshed to main memory
② Update the value of the latest shared variable in main memory to working memory 2 in
Synchronized Synchronization lock :
Synchronized The synchronization lock mainly guarantees the security of data by blocking Atomicity ;
It can also guarantee visibility : When the thread is locked , Clear working memory first → Copy the latest variable in main memory to working memory → Finished executing the code → Refresh the value of the changed shared variable to main memory → Release the mutex .
principle :
- Every Java object It's all related to a monitor object , Add synchronized After and keyword monitor The object will take effect , By entering and exiting Monitor Objects implement mutual exclusion 、 sync ;
- monitor Divided into Owner Indicates the owner of the monitor , At the same time, only one thread can become Owner ;
- At first Monitor in Owner by null, When a thread t1 When accessing the object Owner It's empty , Will perform Monitor.Enter Instructions , The thread will occupy this object Owner;
- When a thread t2 To access object discovery Owner It's locked , be t2 Will enter EntryList Waiting in line ( Blocked state ), When t1 After execution, it will be executed Monitor.Exit command , The thread releases the Owner, notice EntryList Threads to compete Owner

Lock reentry :
Execute multiple times on the same object Monitor.Enter-------- Lock reentry
In order to avoid lock reentry increasing overhead , It can make synchronized The locking granularity is larger , Less Monitor.Enter and Monitor.Exit The number of times
Be careful :
1.synchronized( ) Objects of reference type should be placed inside , You can't put basic types ,
2. No addition synchronized Is not associated with the monitor
边栏推荐
猜你喜欢

Leetcode hot topic hot52-100

对接湖南CA使用U_KEY登录

BroadCast(广播)

vite3学习记录

High numbers | calculation of double integral 3 | high numbers | handwritten notes

集群聊天服务器:如何解决跨服务器通信问题 | redis发布-订阅

LeetCode热题 HOT52-100

《迷失》stray工人帽子获得方法 工人安全帽在哪里?

1309_ Add GPIO flip on STM32F103 and schedule test with FreeRTOS

Connect with Hunan Ca and use U_ Key login
随机推荐
CMake的学习
集群聊天服务器:chatService业务层
(note) learning rate setting of optimizer Adam
TCP半连接队列和全连接队列(史上最全)
SQLite数据库
【攻防世界WEB】难度四星12分进阶题:FlatScience
剑指 Offer II 115. 重建序列 : 拓扑排序构造题
High numbers | calculation of double integral 2 | high numbers | handwritten notes
High numbers | calculation of double integral 4 | high numbers | handwritten notes
【攻防世界WEB】难度四星12分进阶题:Confusion1
手机股票开户安全吗?
Junior intern, ByteDance, after sharing, has been offered
做一个有职业操守的软件匠人
Scala programming (intermediate advanced experimental application)
Protocol buffers 的问题和滥用
【arxiv】第一次上传论文小记
1309_STM32F103上增加GPIO的翻转并用FreeRTOS调度测试
【微信小程序】你了解小程序开发吗?
UnauthorizedAccessException:Access to the path “/xx/xx.xx“ is denied
Comment présenter votre expérience de projet lors d'une entrevue