Synchronized代码小测试
做本实验需要引入坐标
<dependency> <groupId>org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> <version>0.9</version> </dependency>
先看一下我的代码
public static void main(String[] args) throws InterruptedException { Object lightObject = new Object(); try { Thread.sleep(6000L); } catch (InterruptedException e) { e.printStackTrace(); } Object biasedLockObject = new Object(); System.out.println("---------------------------------------加锁前---------------------------------------"); System.out.println("偏向锁:" + ClassLayout.parseInstance(biasedLockObject).toPrintable() + "\n轻量级锁:" + ClassLayout.parseInstance(lightObject).toPrintable()); System.out.println("---------------------------------------加锁后---------------------------------------"); // synchronized (biasedLockObject) { // System.out.println("偏向锁:" + ClassLayout.parseInstance(biasedLockObject).toPrintable()); // } synchronized (lightObject) { System.out.println("轻量级锁第一次:" + ClassLayout.parseInstance(lightObject).toPrintable()); try { Thread.sleep(2000); } catch (InterruptedException e) { } } // System.out.println("---------------------------------------释放锁---------------------------------------"); // System.out.println("偏向锁:" + ClassLayout.parseInstance(biasedLockObject).toPrintable() + "\n轻量级锁:" + ClassLayout.parseInstance(lightObject).toPrintable()); for (int i=0;i<10;i++) { Thread thread = new Thread(new Runnable() { @Override public void run() { // synchronized (biasedLockObject) { // System.out.println("竞争偏向锁:" + ClassLayout.parseInstance(biasedLockObject).toPrintable()); // } // // System.out.println("竞争偏向锁释放:" + ClassLayout.parseInstance(biasedLockObject).toPrintable()); synchronized (lightObject) { System.out.println("竞争轻量级锁:" + ClassLayout.parseInstance(lightObject).toPrintable()); } // System.out.println("竞争偏向锁释放:" + ClassLayout.parseInstance(biasedLockObject).toPrintable()); } }); thread.start(); } Thread.sleep(5000); System.out.println("轻量级锁释放后:" + ClassLayout.parseInstance(lightObject).toPrintable()); }
代码很好懂,就是开10个线程去竞争 lightObject
1 --------------------------------------加锁前--------------------------------------- 2 偏向锁:java.lang.Object object internals: 3 OFFSET SIZE TYPE DESCRIPTION VALUE 4 0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5) 5 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 6 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 7 12 4 (loss due to the next object alignment) 8 Instance size: 16 bytes 9 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 10 11 轻量级锁:java.lang.Object object internals: 12 OFFSET SIZE TYPE DESCRIPTION VALUE 13 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 14 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 15 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 16 12 4 (loss due to the next object alignment) 17 Instance size: 16 bytes 18 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 19 20 ---------------------------------------加锁后--------------------------------------- 21 轻量级锁第一次:java.lang.Object object internals: 22 OFFSET SIZE TYPE DESCRIPTION VALUE 23 0 4 (object header) 50 f3 9d 03 (01010000 11110011 10011101 00000011) (60683088) 24 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 25 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 26 12 4 (loss due to the next object alignment) 27 Instance size: 16 bytes 28 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 29 30 竞争轻量级锁:java.lang.Object object internals: 31 OFFSET SIZE TYPE DESCRIPTION VALUE 32 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 33 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 34 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 35 12 4 (loss due to the next object alignment) 36 Instance size: 16 bytes 37 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 38 39 竞争轻量级锁:java.lang.Object object internals: 40 OFFSET SIZE TYPE DESCRIPTION VALUE 41 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 42 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 43 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 44 12 4 (loss due to the next object alignment) 45 Instance size: 16 bytes 46 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 47 48 竞争轻量级锁:java.lang.Object object internals: 49 OFFSET SIZE TYPE DESCRIPTION VALUE 50 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 51 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 52 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 53 12 4 (loss due to the next object alignment) 54 Instance size: 16 bytes 55 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 56 57 竞争轻量级锁:java.lang.Object object internals: 58 OFFSET SIZE TYPE DESCRIPTION VALUE 59 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 60 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 61 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 62 12 4 (loss due to the next object alignment) 63 Instance size: 16 bytes 64 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 65 66 竞争轻量级锁:java.lang.Object object internals: 67 OFFSET SIZE TYPE DESCRIPTION VALUE 68 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 69 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 70 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 71 12 4 (loss due to the next object alignment) 72 Instance size: 16 bytes 73 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 74 75 竞争轻量级锁:java.lang.Object object internals: 76 OFFSET SIZE TYPE DESCRIPTION VALUE 77 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 78 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 79 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 80 12 4 (loss due to the next object alignment) 81 Instance size: 16 bytes 82 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 83 84 竞争轻量级锁:java.lang.Object object internals: 85 OFFSET SIZE TYPE DESCRIPTION VALUE 86 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 87 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 88 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 89 12 4 (loss due to the next object alignment) 90 Instance size: 16 bytes 91 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 92 93 竞争轻量级锁:java.lang.Object object internals: 94 OFFSET SIZE TYPE DESCRIPTION VALUE 95 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 96 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 97 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 98 12 4 (loss due to the next object alignment) 99 Instance size: 16 bytes 100 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 101 102 竞争轻量级锁:java.lang.Object object internals: 103 OFFSET SIZE TYPE DESCRIPTION VALUE 104 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 105 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 106 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 107 12 4 (loss due to the next object alignment) 108 Instance size: 16 bytes 109 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 110 111 竞争轻量级锁:java.lang.Object object internals: 112 OFFSET SIZE TYPE DESCRIPTION VALUE 113 0 4 (object header) ca 83 73 18 (11001010 10000011 01110011 00011000) (410223562) 114 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 115 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 116 12 4 (loss due to the next object alignment) 117 Instance size: 16 bytes 118 Space losses: 0 bytes internal + 4 bytes external = 4 bytes total 119 120 轻量级锁释放后:java.lang.Object object internals: 121 OFFSET SIZE TYPE DESCRIPTION VALUE 122 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 123 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 124 8 4 (object header) e5 21 e4 27 (11100101 00100001 11100100 00100111) (669262309) 125 12 4 (loss due to the next object alignment)
结论
1 jvm启动的前四秒内创建的对象初始状态就是 00000001 无锁状态
2 第一次竞争 对应23行 01010000 轻量级锁
3 多线程竞争开始 从30行到112行锁的类型都是重量级锁 11001010
4 最后锁释放后 120行 00000001 无锁状态
从实验结果能看得出来,锁是会降级的,而不是网络上好多人说的,锁不会降级。另外,再次强调下,无锁绝对不会到偏向锁。