当前位置:网站首页>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
边栏推荐
- 一道golang中关于for range常见坑的面试题
- 集群聊天服务器:网络模块ChatServer
- [continuous update] collection of raspberry pie startup and failure series
- Cmake learning
- Green Tao theorem (4): energy increment method
- High numbers | calculation of double integral 4 | high numbers | handwritten notes
- Kubevela offline installation
- h264编码参数
- Scala programming (elementary)
- 2022.7.22 js对象
猜你喜欢

合宙ESP32C3硬件配置信息串口打印输出

Oom mechanism

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

Day109.尚医通:集成Nacos、医院列表、下拉列表查询、医院上线功能、医院详情查询

【着色器实现RoundWave圆形波纹效果_Shader效果第六篇】

Stm32c8t6 driving lidar actual combat (II)

WinDbg practice -- Introduction

集群聊天服务器:集群与分布式理论

Network learning infrared module, 8-way emission independent control

集群聊天服务器:Model数据层的框架设计和数据库代码的封装
随机推荐
1309_ Add GPIO flip on STM32F103 and schedule test with FreeRTOS
Unity solves that animation is not available: the animationclip 'xxx' used by the animation component 'xxx' must be marked as legacy
Chapter 2 回归
初识js(适合新手的编程)
High numbers | calculation of double integral 2 | high numbers | handwritten notes
scala编程(中级进阶实验应用)
《迷失》stray工人帽子获得方法 工人安全帽在哪里?
2022.7.22 js对象
集群聊天服务器:Model数据层的框架设计和数据库代码的封装
Jetson nano recording stepping on the pit (it will definitely solve your problem)
Major optimization of openim - Message loading on demand, consistent cache, uniapp Publishing
LeetCode热题 HOT52-100
scala編程(初級)
确定括号序列中的一些位置
Oom mechanism
Cluster chat server: Design of database table
Jianzhi offer II 115. reconstruction sequence: topological sorting construction problem
如何在面試中介紹自己的項目經驗
TCP half connection queue and full connection queue (the most complete in History)
集群聊天服务器:工程目录的创建