动态

详情 返回 返回

鎖狀態標識位 - 动态 详情

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
    

Add a new 评论

Some HTML is okay.