主要通过代码,观察synchronized时,锁升级,markword的变化
借助工具
- jol(参考:https://www.jianshu.com/p/1b370f36777f)
-
markword结构图
代码
public class BiasedLock {
public static void main(String[] args) throws Exception {
System.out.println(org.openjdk.jol.vm.VM.current().details());
// 必要,不然不会观察到偏向锁,去掉的话,会观察到直接由无锁到轻量级锁
TimeUnit.SECONDS.sleep(5);
Object obj = new Object();
System.out.println(ClassLayout.parseInstance(obj).toPrintable());
lock(obj);
for (int i = 0; i < 3; i++) {
new Thread(() -> lock(obj)).start();
}
}
private static void lock(Object obj) {
synchronized (obj) {
System.out.println(ClassLayout.parseInstance(obj).toPrintable());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
观察结果
object header中
00000101 00000000 00000000 00000000 (偏向锁)
->
00110000 11101101 10111101 01011011 (轻量级锁)
->
11011010 11000101 00111000 01010111 (重量级锁)
# Running 64-bit HotSpot VM.
# Using compressed oop with 0-bit shift.
# Using compressed klass with 0-bit shift.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
java.lang.Object object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 28 0f 9f 16 (00101000 00001111 10011111 00010110) (379522856)
12 4 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
java.lang.Object object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 05 e0 40 02 (00000101 11100000 01000000 00000010) (37806085)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 28 0f 9f 16 (00101000 00001111 10011111 00010110) (379522856)
12 4 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
java.lang.Object object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 30 ed bd 5b (00110000 11101101 10111101 01011011) (1539173680)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 28 0f 9f 16 (00101000 00001111 10011111 00010110) (379522856)
12 4 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
java.lang.Object object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) da c5 38 57 (11011010 11000101 00111000 01010111) (1463338458)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 28 0f 9f 16 (00101000 00001111 10011111 00010110) (379522856)
12 4 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
java.lang.Object object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) da c5 38 57 (11011010 11000101 00111000 01010111) (1463338458)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 28 0f 9f 16 (00101000 00001111 10011111 00010110) (379522856)
12 4 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total