当前位置:网站首页>How to realize synchronized
How to realize synchronized
2022-07-23 08:19:00 【LLAiden】
synchronized It's the code used by many friends, and it's different from keywords , Let's take a look synchronized How is it implemented at the bytecode level
On the first code
public class Main {
public static void main(String[] args) throws InterruptedException {}
public void m() {
Object object = new Object();
synchronized (object) {
}
}
}This code actually has no task logic , There's one m Method , And there is an empty in the method synchronized Code block
Let's use idea Look at bytecode with the tools in view->show Bytecode
public m()V
TRYCATCHBLOCK L0 L1 L2 null
TRYCATCHBLOCK L2 L3 L2 null
L4
LINENUMBER 12 L4
NEW java/lang/Object
DUP
INVOKESPECIAL java/lang/Object.<init> ()V
ASTORE 1
L5
LINENUMBER 13 L5
ALOAD 1
DUP
ASTORE 2
MONITORENTER
L0
LINENUMBER 14 L0
ALOAD 2
MONITOREXIT
L1
GOTO L6
L2
FRAME FULL [org/example/Main java/lang/Object java/lang/Object] [java/lang/Throwable]
ASTORE 3
ALOAD 2
MONITOREXIT
L3
ALOAD 3
ATHROW
L6
LINENUMBER 15 L6
FRAME CHOP 1
RETURN
L7
LOCALVARIABLE this Lorg/example/Main; L4 L7 0
LOCALVARIABLE object Ljava/lang/Object; L5 L7 1
MAXSTACK = 2
MAXLOCALS = 4
Let's focus on the following code
MONITORENTER
L0
LINENUMBER 13 L0
ALOAD 1
MONITOREXITHere it is synchronized In the implementation of bytecode level, let's see the removal synchronized Bytecode after block
public m()V
L0
LINENUMBER 12 L0
NEW java/lang/Object
DUP
INVOKESPECIAL java/lang/Object.<init> ()V
ASTORE 1
L1
LINENUMBER 13 L1
RETURN
L2
LOCALVARIABLE this Lorg/example/Main; L0 L2 0
LOCALVARIABLE object Ljava/lang/Object; L1 L2 1
MAXSTACK = 2
MAXLOCALS = 2It is obvious from the two byte codes MONITORENTER and MONITOREXIT Removed , From here we can know synchronized Implementation at bytecode level
synchronized The synchronization code block is implemented at the bytecode level as follows
MONITORENTER
...
Our synchronization code block
...
MONITOREXIT边栏推荐
- 挖财和启牛都是干什么的开户安全吗?
- 1.11 ArrayList & student management system
- mysql使用sql语句查询某个字段值除10等于0的所有数据
- leetcode-382.链表随机节点
- Spark troubleshooting -precondition eof: no length prefix available
- Internet traffic scheduling scheme
- y74.第四章 Prometheus大厂监控体系及实战 -- PromQL简介和监控pod资源(五)
- buu web
- Wechat applet project practice
- 高精度移相(MCP41xx)程序stm32F103,F407通用,更改引脚即可(SPI软件模拟通信)
猜你喜欢
随机推荐
VMware virtual machine changes static IP and reports an error unit network Service entered failed state solution
将childNodes返回的伪数组转化为真数组
JS regular delete the span tag and the content in the tag
C语言中的字符串
笔者认为,元宇宙与互联网的发展逻辑在某种成都上是截然不同的
获取一个控件宽度
Typora设置标题自动添加序号
Learn these SketchUp skills and improve work efficiency by half
二叉树(学习日常)
重链剖分例题配套题解的题解
深度解析kube-scheduler调度上下文
C language decimal number to binary number
动作捕捉在自动化控制领域的应用
y74.第四章 Prometheus大厂监控体系及实战 -- PromQL简介和监控pod资源(五)
TensorRT的插件实战(1)
js 正则删除span标签以及标签里面的内容
Redis 事务学习有感
Redis事务与锁机制
Genesis公链:夯实Web 3.0发展底座
H7-TOOL串口脱机烧录操作说明,支持TTL串口,RS232和RS485(2022-06-30)









