当前位置:网站首页>Synchronized and volatile interview brief summary
Synchronized and volatile interview brief summary
2022-07-31 14:58:00 【tall boy】
synchronized
Synchronized的作用主要有三个:
确保线程互斥的访问同步代码
保证共享变量的修改能够及时可见
有效解决重排序问题
从语法上讲,Synchronized总共有三种用法:
修饰普通方法
修饰静态方法
修饰代码块
synchronized修饰普通方法时,Use the lock is object lock.
synchronized修饰静态方法时,Use the lock is a class object lockClass.
synchronized修饰的代码块 Use lock is object lock.
监视者monitorenter
When object occupied it becomes locked,线程执行monitorenter指令时尝试获取monitor的所有权,过程如下:
如果monitorEnter the number of0,则该线程进入monitor,And then set into the digital to1,该线程即为monitor的所有者.
如果线程已经占有该monitor,只是重新进入,Display to enter the number of1.
如果其他线程已经占用了monitor,Then the thread into two-way,直到monitor的进入数为0,再重新尝试获取monitor的所有权.
监控退出monitorexit
执行monitorexit的线程必须是objectref所对应的monitor的所有者.
指令执行时,monitor的进入数减1,如果减1后进入数为0,那线程退出monitor,不再是这个monitor的所有者.其他被该monitorThe thread can be to try to get thismonitor的所有权.
通过这两段描述,We should be able to clear clipSynchronized的实现原理,SynchronizedThe semantics of the agreement is through amonitor的对象来完成,Actually waiting for/Notification method also depends on themonitor对象,This is why only in the synchronized block or method can callwait / notify等方法,否则会引发java.lang.IllegalMonitorStateException的异常的原因.
Synchronized底层实现方式
同步代码块

synchronized映射成字节码指令就是增加来两个指令:monitorenter和monitorexit.When a thread to execute the metmonitorenter指令的时候,它会去尝试获得锁,If get lock lock count+1(Why add a,因为它是一个可重入锁,所以需要用这个锁计数判断锁的情况),如果没有获得锁,那么阻塞.当它遇到monitorexit的时候,锁计数器-1,当计数器为0,那么就释放锁.
图上有2个monitorexit,synchronized锁释放有两种机制,一种就是执行完释放;Another is to send the unusual,虚拟机释放.图中第二个monitorexit就是发生异常时执行的流程,就是“会有2There is a process“.而且,从图中我们也可以看到在第13行,有一个goto指令,也就是说如果正常运行结束会跳转到19行执行.
同步方法
方法的同步并没有通过指令monitorenter和monitorexit来完成(理论上其实也可以通过这两条指令来实现),不过相对于普通方法,其常量池中多了ACC_SYNCHRONIZED标示符.
JVMIs the realization method of synchronization according to the identifier when a method call,调用指令将会检查方法的 ACC_SYNCHRONIZED 访问标志是否被设置,如果设置了,执行线程将先获取monitor,获取成功之后才能执行方法体,方法执行完后再释放monitor.
在方法执行期间,其他任何线程都无法再获得同一个monitor对象.其实本质上没有区别,只是方法的同步是一种隐式的方式来实现,无需通过字节码来完成.
synchronized与Lock的区别
类别 | synchronized | Lock |
存在层次 | Java的关键字,在jvm层面上 | 是一个类 |
锁的释放 | 1、以获取锁的线程执行完同步代码,释放锁 2、线程执行发生异常,jvm会让线程释放锁 | 在finally中必须释放锁,不然容易造成线程死锁 |
锁的获取 | 假设A线程获得锁,B线程等待.如果A线程阻塞,B线程会一直等待 | 分情况而定,Lock有多个锁获取的方式,大致就是可以尝试获得锁,线程可以不用一直等待 |
锁状态 | 无法判断 | 可以判断 |
锁类型 | 可重入 不可中断 非公平 | 可重入 可判断 可公平(两者皆可) |
性能 | 少量同步 | 大量同步 |
wait()与sleep()的区别,简单来说wait()Will release the object lock andsleep()不会释放对象锁.
volatile
内存可见性
Java 内存模型(JMM):在 Java 中所有的共享变量都在主内存中,每个线程都有自己的工作内存,为了提高线程的运行速度,每个线程的工作内存都会把主内存中的共享变量拷贝一份进行缓存,以此来提高运行效率.但这样就会产生一个新的问题,如果某个线程修改了共享变量的值,其他线程不知道此值被修改了,就会发生两个线程值不一致的情况,
The visibility of memory refers to the thread after modify the value of the variable,其他线程能立即知道此值发生了改变.
volatile 只是轻量级的线程可见方式,并不是轻量级的同步方式,所以并不能说 volatile 是轻量级的 synchronized
内存屏障的3个功能:
I. 它确保指令重排序时不会把其后面的指令排到内存屏障之前的位置,也不会把前面的指令排到内存屏障的后面;即在执行到内存屏障这句指令时,在它前面的操作已经全部完成;
II. 它会强制将对缓存的修改操作立即写入主存;
III. 如果是写操作,它会导致其他CPU中对应的缓存行无效.
边栏推荐
- 学习笔记12--路径-速度分解法之局部路径搜索
- The meaning of node_exporter performance monitoring information collection in Prometheus
- charles进行弱网测试(app弱网测试怎么做)
- 力扣:738.单调递增的数字
- OpenCV测量物体的尺寸技能 get~
- Resnet&API
- OAuth2:单点登陆客户端
- 英文语法-时与态
- R language ggplot2 visualization: use the ggboxplot function of the ggpubr package to visualize the box plot, use the font function to customize the font size, color, style (bold, italic) of the legen
- 蔚来杯2022牛客暑期多校训练营4
猜你喜欢
随机推荐
思路迪医药冲刺港股:5个月亏2.9亿 泰格医药与先声药业是股东
谷歌CTS测试(cta测试)
The meaning of node_exporter performance monitoring information collection in Prometheus
什么是消息队列呢?
Sentinel服务熔断和降级
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(caption、添加、修改可视化图像的题注、脚注内容)
最小费用最大流问题详解
OpenShift 4 - Deploy Redis Cluster with Operator
OAuth2:资源服务器
消息队列消息数据存储MySQL表设计
小试牛刀:Go 反射帮我把 Excel 转成 Struct
Efficient use of RecyclerView Section 3
OAuth2:使用JWT令牌
名创优品斥资6.95亿购买创始人叶国富所持办公楼股权
自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统
OAuth2:单点登陆客户端
DeepLab Series Learning
leetcode303场周赛复盘
Redis与分布式:主从复制
如何进行需求分析评审