synchronized 代码块是由一对 monitorenter/monitorexit 指令实现的,Monitor 对象是同步的基本实现单元。
在Java6之前, Monitor的实现完全是依靠操作系统内部的互斥锁,因为需要进行用户态到内核态的切换,所以同步操作是一个无差别的重量级操作。现代的( Oracle)JDK中,JVM对此进行了大刀阔斧地改进,提供了三种不同的 Monitor 实现,也就是常说的三种不同的锁:偏斜锁( Biased Locking)、轻量级锁和重量级锁,大大改进了其性能。
所谓的锁升级、降级,就是 JVM 优化 synchronized 运行的机制,当 JVM 监测到不同的竞争状况是,会自动切换到不同的锁实现。这种切换就是锁的升级、降级。
说偏向锁之前,需要理解对象的结构,对象由多部分构成的,对象头,属性字段、补齐区域等。所谓补齐区域是指如果对象总大小不是4字节的整数倍,会填充上一段内存地址使之成为整数倍。
image
偏向锁又和对象头密切相关,对象头这部分在对象的最前端,包含两部分或者三部分:Mark Words、Klass Words,如果对象是一个数组,那么还可能包含第三部分:数组的长度。
如果应用的对象过多,使用64位的指针将浪费大量内存。64位的JVM比32位的JVM多耗费50%的内存。我们现在使用的64位 JVM会默认使用选项 +UseCompressedOops 开启指针压缩,将指针压缩至32位。
以64位操作系统为例,对象头存储内容图例
|--------------------------------------------------------------------------------------------------------------|
| Object Header (128 bits) |
|--------------------------------------------------------------------------------------------------------------|
| Mark Word (64 bits) | Klass Word (64 bits) |
|--------------------------------------------------------------------------------------------------------------|
| unused:25 | identity_hashcode:31 | unused:1 | age:4 | biased_lock:1 | lock:2 | OOP to metadata object | 无锁
|----------------------------------------------------------------------|--------|------------------------------|
| thread:54 | epoch:2 | unused:1 | age:4 | biased_lock:1 | lock:2 | OOP to metadata object | 偏向锁
|----------------------------------------------------------------------|--------|------------------------------|
| ptr_to_lock_record:62 | lock:2 | OOP to metadata object | 轻量锁
|----------------------------------------------------------------------|--------|------------------------------|
| ptr_to_heavyweight_monitor:62 | lock:2 | OOP to metadata object | 重量锁
|----------------------------------------------------------------------|--------|------------------------------|
| | lock:2 | OOP to metadata object | GC
|--------------------------------------------------------------------------------------------------------------|
对象头中的信息如何理解呢,举个例子
image
从该对象头中分析加锁信息,MarkWordk为0x0000700009b96910,二进制为0xb00000000 00000000 01111111 11110000 11001000 00000000 01010011 11101010。倒数第三位为"0",说明不是偏向锁状态,倒数两位为"10",因此,是重量级锁状态,那么前面62位就是指向互斥量的指针。
basied_lock | lock | 状态 |
---|---|---|
0 | 01 | 无锁 |
1 | 01 | 偏向锁 |
0 | 00 | 轻量级锁 |
0 | 10 | 重量级锁 |
0 | 11 | GC标记 |
A a = new A();
System.out.println(ClassLayout.parseInstance(a).toPrintable());
可以看到最后 00000001 basied_lock = 0, lock =01 表示无锁
JavaThread.synchronizestructure.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 43 c0 00 20 (01000011 11000000 00000000 00100000) (536920131)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
当没有竞争出现时,默认使用偏斜锁。JVM 会利用 CAS 操作在对象头上的 Mark Word 部分设置线程 ID ,以表示对象偏向当前线程。所以并不涉及真正的互斥锁,这样做的假设是基于在很多应用场景中,大部分对象生命周期中最多会被一个线程锁定,使用偏斜锁可以降低无竟争开销。测试代码:
Thread.sleep(5000);
A a = new A();
synchronized (a) {
System.out.println(ClassLayout.parseInstance(a).toPrintable());
}
运行结果:
JavaThread.synchronizestructure.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 05 28 8d 02 (00000101 00101000 10001101 00000010) (42805253)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 43 c0 00 20 (01000011 11000000 00000000 00100000) (536920131)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
00000101 中 basied_lock = 1, lock =01 表示偏斜锁
thread1中依旧输出偏向锁,主线程获取对象A时,thread1虽然已经退出同步代码块,但主线程和thread1仍然为锁的交替竞争关系。故此时主线程输出结果为轻量级锁。测试代码:
Thread.sleep(5000);
A a = new A();
Thread thread1= new Thread(){
@Override
public void run() {
synchronized (a){
System.out.println("thread1 locking");
System.out.println(ClassLayout.parseInstance(a).toPrintable()); //偏向锁
}
}
};
thread1.start();
thread1.join();
Thread.sleep(10000);
synchronized (a){
System.out.println("main locking");
System.out.println(ClassLayout.parseInstance(a).toPrintable());//轻量锁
}
}
运行结果:
JavaThread.synchronizestructure.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 05 40 e9 19 (00000101 01000000 11101001 00011001) (434716677)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) a0 c0 00 20 (10100000 11000000 00000000 00100000) (536920224)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
main locking
JavaThread.synchronizestructure.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 38 f6 c7 02 (00111000 11110110 11000111 00000010) (46659128)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) a0 c0 00 20 (10100000 11000000 00000000 00100000) (536920224)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
00000101 依然是偏向锁,00111000 是轻量级锁
thread1 和 thread2 同时竞争对象a,此时输出结果为重量级锁
测试代码:
Thread.sleep(5000);
A a = new A();
Thread thread1 = new Thread(){
@Override
public void run() {
synchronized (a){
System.out.println("thread1 locking");
System.out.println(ClassLayout.parseInstance(a).toPrintable());
try {
//让线程晚点儿死亡,造成锁的竞争
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread2 = new Thread(){
@Override
public void run() {
synchronized (a){
System.out.println("thread2 locking");
System.out.println(ClassLayout.parseInstance(a).toPrintable());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread1.start();
thread2.start();
运行结果:
thread2 locking
JavaThread.synchronizestructure.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 7a f5 99 17 (01111010 11110101 10011001 00010111) (395965818)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 62 c1 00 20 (01100010 11000001 00000000 00100000) (536920418)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
01111010 basied_lock = 0 lock=10 重量级锁