Object o = new Object();
占用几个字节;16个;
8个字节markWord,4个classPointer,4个字节对齐;
synchronize不同状态对应的对象头状态:
synchronized的实现:
- Java代码:synchronized关键字;
- 字节码:monitorenter monitorExit
- JVM:锁的升级过程
- 硬件:lock指令和 comxchg指令 (compare and exchange)
lock指令:在执行后面指令的时候,锁定北桥信号,不采用锁总线的方式;
// access flags 0x1
public add()V
TRYCATCHBLOCK L0 L1 L2 null
TRYCATCHBLOCK L2 L3 L2 null
L4
LINENUMBER 34 L4
ALOAD 0
DUP
ASTORE 1
MONITORENTER
L0
LINENUMBER 35 L0
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
INVOKESTATIC java/lang/Thread.currentThread ()Ljava/lang/Thread;
INVOKEVIRTUAL java/lang/Thread.getName ()Ljava/lang/String;
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
L5
LINENUMBER 36 L5
GETSTATIC com/sliu/offer/didinew/JianzhiOffer.i : I
ICONST_1
IADD
PUTSTATIC com/sliu/offer/didinew/JianzhiOffer.i : I
L6
LINENUMBER 37 L6
ALOAD 1
MONITOREXIT