1

image.png

image.png

锁升级过程

      public static void main(String[] args) throws Exception {
            Object lock = new Object();
            System.out.println("A--->" + ClassLayout.parseInstance(lock).toPrintable());
    
            synchronized (lock) {
                System.out.println("B--->" + ClassLayout.parseInstance(lock).toPrintable());
            }
    
            Thread.sleep(5000);
    
            Object object = new Object();
            System.out.println("C--->" + ClassLayout.parseInstance(object).toPrintable());
    
            new Thread(() -> {
                synchronized (object) {
                    System.out.println("D--->" + ClassLayout.parseInstance(object).toPrintable());
                }
            }).start();
    
            synchronized (object) {
                System.out.println("E--->" + ClassLayout.parseInstance(object).toPrintable());
            }
        }

  1. Jvm启动,由于偏向锁延时4秒开启,此时尚未开启偏向锁,创建的对象锁状态为 001,对象为普通对象

     Object lock = new Object();
     System.out.println("A--->" + ClassLayout.parseInstance(lock).toPrintable());
    
     A--->java.lang.Object 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)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
          12     4        (loss due to the next object alignment)
     Instance size: 16 bytes
     Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    
    
  2. 对lock对象加锁,还未开启偏向锁,锁状态为 00,轻量级锁

     synchronized (lock) {
          System.out.println("B--->" + ClassLayout.parseInstance(lock).toPrintable());
     }
     B--->java.lang.Object object internals:
     OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
           0     4        (object header)                           98 ff db 0c (10011000 11111111 11011011 00001100) (215744408)
           4     4        (object header)                           00 70 00 00 (00000000 01110000 00000000 00000000) (28672)
           8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
          12     4        (loss due to the next object alignment)
     Instance size: 16 bytes
     Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    
  3. 线程sleep5秒,开启偏向锁

     Thread.sleep(5000);
    
  4. 偏向锁已开启,此时创建的对象为匿名偏向对象,即对象的锁状态为101,包含了偏向锁标志位,但是偏向线程id为空

             Object object = new Object();
     System.out.println("C--->" + ClassLayout.parseInstance(object).toPrintable());
             
             C--->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)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
          12     4        (loss due to the next object alignment)
     Instance size: 16 bytes
     Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    
  5. 锁状态表示为是10,重量级锁,偏向锁直接升级为重量级锁

     new Thread(() -> {
        synchronized (object) {
            System.out.println("D--->" + ClassLayout.parseInstance(object).toPrintable());
        }
     }).start();
     
     D--->java.lang.Object object internals:
     OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
           0     4        (object header)                           ea d7 02 ec (11101010 11010111 00000010 11101100) (-335357974)
           4     4        (object header)                           d8 7f 00 00 (11011000 01111111 00000000 00000000) (32728)
           8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
          12     4        (loss due to the next object alignment)
     Instance size: 16 bytes
     Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    

一只鱼
49 声望1 粉丝

« 上一篇
synchronized
下一篇 »
volatile

引用和评论

0 条评论