• 对象头markword-锁升级


    锁状态的“可视化”实践
    • 通过工具查看对象的内存布局
    MAVEN引入
    1. <dependency>
    2.   <groupId>org.openjdk.jolgroupId>
    3.   <artifactId>jol-coreartifactId>
    4.   <version>0.10version>  
    5. dependency>
    1. package cn.ly.test;
    2. import org.openjdk.jol.info.ClassLayout;
    3. public class JavaObjLayout {
    4.     int[] aa = new int[3];
    5.     public static void main(String[] args) {
    6.         A a = new A();
    7.         B b = new B();
    8.         C c = new C();
    9.         int[] aa = new int[3];
    10.         //查看对象内部信息`
    11.         System.out.println(ClassLayout.
    12.                 parseInstance(a).toPrintable());
    13.         System.out.println(ClassLayout.
    14.                 parseInstance(b).toPrintable());
    15.         System.out.println(ClassLayout.
    16.                 parseInstance(c).toPrintable());
    17.         System.out.println(ClassLayout.
    18.                 parseInstance(aa).toPrintable());
    19.     }
    20.     static class A {
    21.     }
    22.     static class B {
    23.         private long s;
    24.     }
    25.     static class C {
    26.         private int a;
    27.         private long d;
    28.     }
    29. }
    30. ################################################################################################################################################################################
    31. # WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
    32. cn.ly.test.JavaObjLayout$A object internals:
    33. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    34.       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
    35.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    36.       8     4        (object header)                           44 c1 00 f8 (01000100 11000001 00000000 11111000) (-134168252)
    37.      12     4        (loss due to the next object alignment)
    38. Instance size: 16 bytes
    39. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    40. cn.ly.test.JavaObjLayout$B object internals:
    41. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    42.       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
    43.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    44.       8     4        (object header)                           83 c1 00 f8 (10000011 11000001 00000000 11111000) (-134168189)
    45.      12     4        (alignment/padding gap)                  
    46.      16     8   long B.s                                       0
    47. Instance size: 24 bytes
    48. Space losses: 4 bytes internal + 0 bytes external = 4 bytes total
    49. cn.ly.test.JavaObjLayout$C object internals:
    50. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    51.       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
    52.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    53.       8     4        (object header)                           c1 c1 00 f8 (11000001 11000001 00000000 11111000) (-134168127)
    54.      12     4    int C.a                                       0
    55.      16     8   long C.d                                       0
    56. Instance size: 24 bytes
    57. Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
    58. [I object internals:
    59. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    60.       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
    61.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    62.       8     4        (object header)                           6d 01 00 f8 (01101101 00000001 00000000 11111000) (-134217363)
    63.      12     4        (object header)                           03 00 00 00 (00000011 00000000 00000000 00000000) (3)
    64.      16    12    int [I.                             N/A
    65.      28     4        (loss due to the next object alignment)
    66. Instance size: 32 bytes
    67. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    68. ################################################################################################################################################################################

    注意点:
    1. jdk6默认开启偏向锁,但是是输入延时开启,也就是说,程序刚启动创建的对象是不会开启偏向锁的,几秒后后创建的对象才会开启偏向锁的
    //关闭延迟开启偏向锁
    -XX:BiasedLockingStartupDelay=0
    //禁止偏向锁
    -XX:-UseBiasedLocking
    //启用偏向锁
    -XX:+UseBiasedLocking
        
    1. Jol layout 结果查看需要注意顺序
    对象头看锁升级过程:
    一、无锁状态和偏向锁状态
    1. JVM的启动参数默认值。偏向锁相关的不做任何设置。这意味着默认会有几秒的锁偏向的延迟。
    2. 代码示例,及结果
        
    1. public static void main(String[] args) throws InterruptedException {
    2.     Object obj1 = new Object();
    3.     // 由于偏向锁生效是有延迟的(JVM启动后相对时间),所以这里采用这种方式来演示延迟
    4.     // 在偏向锁生效前先创建好一个对象 obj1 ,此时对象头默认是无锁状态:001
    5.     TimeUnit.SECONDS.sleep(5);
    6.     // sleep的目的是为了使"延迟的偏向锁"变得生效
    7.     System.out.println(ClassLayout.
    8.             parseInstance(obj1).toPrintable());
    9.     
    10.     // JVM偏向锁延迟生效以后,再创建对象Obj2这个时候会发现,默认对象的头标记就是偏向锁 101
    11.     Object obj2 = new Object();
    12.     System.out.println(ClassLayout.
    13.             parseInstance(obj2).toPrintable());
    14. }
    15. ################################################################################################################################################################################
    16. # WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
    17. java.lang.Object object internals:
    18. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    19.       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
    20.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    21.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    22.      12     4        (loss due to the next object alignment)
    23. Instance size: 16 bytes
    24. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    25. java.lang.Object object internals:
    26. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    27.       0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
    28.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    29.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    30.      12     4        (loss due to the next object alignment)
    31. Instance size: 16 bytes
    32. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    33. ################################################################################################################################################################################
    注意结果:
    二、无锁膨胀为轻量级锁
    1. JVM参数不做任何调整。即存在偏向锁的延迟.
    2. 代码示例及结果        
    1. /**
    2. * 轻量级锁
    3. */
    4. public static void lightLock() throws InterruptedException {
    5.     Object obj3 = new Object();
    6.     System.out.println("无锁状态下的对象头markword标记:\n" + ClassLayout.
    7.             parseInstance(obj3).toPrintable());
    8.     System.out.println("------------------------------------------------------------------------------------------------------");
    9.     Thread t1 = new Thread(()->{
    10.         synchronized(obj3){
    11.             System.out.println("线程t1获得锁obj3");
    12.             System.out.println("偏向锁延迟生效时间内,无锁状态-膨胀为:轻量锁 ,其对象头markword标记:\n" + ClassLayout.
    13.                     parseInstance(obj3).toPrintable());
    14.         }
    15.     });
    16.     t1.start();
    17.     t1.join();
    18. }
    19. ################################################################################################################################################################################
    20. 无锁状态下的对象头markword标记:
    21. java.lang.Object object internals:
    22. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    23.       0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
    24.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    25.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    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. 线程t1获得锁obj3
    31. 偏向锁延迟生效时间内,无锁状态-膨胀为:轻量锁 ,其对象头markword标记:
    32. java.lang.Object object internals:
    33. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    34.       0     4        (object header)                           08 19 60 08 (00001000 00011001 01100000 00001000) (140515592)
    35.       4     4        (object header)                           00 70 00 00 (00000000 01110000 00000000 00000000) (28672)
    36.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    37.      12     4        (loss due to the next object alignment)
    38. Instance size: 16 bytes
    39. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    40. ################################################################################################################################################################################

    三、偏向锁和轻量锁升级
    1. JVM参数不做任何调整。即存在偏向锁的延迟.(代码中通过sleep来达到让延迟到期从而偏向生效来演示)
    2. 代码和结果如下:
    1. /**
    2. * 偏向锁-轻量级锁,在偏向生效以后
    3. */
    4. public static void lightLockAfterBiasedLockingEffective() throws InterruptedException {
    5.     // 由于偏向锁生效是有延迟的(JVM启动后相对时间),所以这里采用这种方式来演示延迟
    6.     TimeUnit.SECONDS.sleep(5);
    7.     Object obj4 = new Object();
    8.     System.out.println("JVM偏向锁已经生效:偏向锁状态下的对象头markword标记:\n" + ClassLayout.
    9.             parseInstance(obj4).toPrintable());
    10.     System.out.println("------------------------------------------------------------------------------------------------------");
    11.     Thread t2 = new Thread(()->{
    12.         synchronized(obj4){
    13.             System.out.println("线程t2获得锁obj4");
    14.             System.out.println("偏向锁延迟生效后,线程t2 打印obj4 对象偏向锁状态,其对象头markword标记:\n" + ClassLayout.
    15.                     parseInstance(obj4).toPrintable());
    16.         }
    17.     });
    18.     t2.start();
    19.     t2.join();
    20.     TimeUnit.SECONDS.sleep(2);
    21.     System.out.println("------------------------------------------------------------------------------------------------------");
    22.     Thread t3 = new Thread(()->{
    23.         synchronized(obj4){
    24.             System.out.println("线程t3获得锁obj4");
    25.             System.out.println("偏向锁延迟生效后,偏向锁状态-膨胀为:轻量锁 ,其对象头markword标记:\n" + ClassLayout.
    26.                     parseInstance(obj4).toPrintable());
    27.         }
    28.     });
    29.     t3.start();
    30.     t3.join();
    31. }
    32. ################################################################################################################################################################################
    33. JVM偏向锁已经生效:偏向锁状态下的对象头markword标记:
    34. java.lang.Object object internals:
    35. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    36.       0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
    37.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    38.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    39.      12     4        (loss due to the next object alignment)
    40. Instance size: 16 bytes
    41. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    42. ------------------------------------------------------------------------------------------------------
    43. t2 内存地址:31874192952,十六进制位:76bd99638,二进制位:11101101011110110011001011000111000
    44. 线程t2获得锁obj4
    45. 偏向锁延迟生效后,线程t2 打印obj4 对象偏向锁状态,其对象头markword标记:
    46. java.lang.Object object internals:
    47. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    48.       0     4        (object header)                           05 48 95 c8 (00000101 01001000 10010101 11001000) (-929740795)
    49.       4     4        (object header)                           d3 7f 00 00 (11010011 01111111 00000000 00000000) (32723)
    50.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    51.      12     4        (loss due to the next object alignment)
    52. Instance size: 16 bytes
    53. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    54. t2 id:14,二进制位:1110
    55. ------------------------------------------------------------------------------------------------------
    56. 线程t3获得锁obj4
    57. 偏向锁延迟生效后,偏向锁状态-膨胀为:轻量锁 ,其对象头markword标记:
    58. java.lang.Object object internals:
    59. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    60.       0     4        (object header)                           08 b9 cc 0e (00001000 10111001 11001100 00001110) (248297736)
    61.       4     4        (object header)                           00 70 00 00 (00000000 01110000 00000000 00000000) (28672)
    62.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    63.      12     4        (loss due to the next object alignment)
    64. Instance size: 16 bytes
    65. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    66. ################################################################################################################################################################################

       

    三、升级到重量级锁
    1. JVM参数不做任何调整。即存在偏向锁的延迟.(代码中通过sleep来达到让延迟到期从而偏向生效来演示)
    2. 代码和结果如下:
    1. public static void weightAfterBiasedLockingEffective() throws InterruptedException {
    2.     // 由于偏向锁生效是有延迟的(JVM启动后相对时间),所以这里采用这种方式来演示延迟
    3.     TimeUnit.SECONDS.sleep(5);
    4.     Object obj5 = new Object();
    5.     System.out.println("JVM偏向锁已经生效:偏向锁状态下的对象头markword标记:\n" + ClassLayout.
    6.             parseInstance(obj5).toPrintable());
    7.     System.out.println("------------------------------------------------------------------------------------------------------");
    8.     Thread t2 = new Thread(()->{
    9.         synchronized(obj5){
    10.             System.out.println("线程t2获得锁obj4");
    11.             System.out.println("偏向锁延迟生效后,线程t2 打印obj5 对象偏向锁状态,其对象头markword标记:\n" + ClassLayout.
    12.                     parseInstance(obj5).toPrintable());
    13.             long tid = Thread.currentThread().getId();
    14.             System.out.println("t2 id:"+ tid + ",二进制位:" + Long.toBinaryString(tid));
    15.         }
    16.     });
    17.     t2.start();
    18.     long t2Addr =  VM.current().addressOf(t2);
    19.     System.out.println("t2 内存地址:"+ t2Addr + ",十六进制位:" + Long.toHexString(t2Addr)+ ",二进制位:" + Long.toBinaryString(t2Addr));
    20.     t2.join();
    21.     System.out.println("------------------------------------------------------------------------------------------------------");
    22.     TimeUnit.MILLISECONDS.sleep(500);
    23.     Thread t3 = new Thread(()->{
    24.         synchronized(obj5){
    25.             System.out.println("[线程t3] 获得锁obj5");
    26.             System.out.println("偏向锁延迟生效后,偏向锁状态-膨胀为:??? ,其对象头markword标记:\n" + ClassLayout.
    27.                     parseInstance(obj5).toPrintable());
    28.         }
    29.     });
    30.     t3.start();
    31.     Thread t4 = new Thread(()->{
    32.         synchronized(obj5){
    33.             System.out.println("[线程t4] 获得锁obj5");
    34.             System.out.println("偏向锁延迟生效后,偏向锁状态-膨胀为:??? ,其对象头markword标记:\n" + ClassLayout.
    35.                     parseInstance(obj5).toPrintable());
    36.         }
    37.     });
    38.     t4.start();
    39. }
    备注:升级重量级锁的过程稍微复杂一些,会根据线程的调度情况和JVM运行时的情况不一样产生三种不一样的结果。
    以下是多次跑代码后得到的3种不一样的结果
    1. ##########################################################################情况一######################################################################################################
    2. JVM偏向锁已经生效:偏向锁状态下的对象头markword标记:
    3. java.lang.Object object internals:
    4. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    5.       0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
    6.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    7.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    8.      12     4        (loss due to the next object alignment)
    9. Instance size: 16 bytes
    10. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    11. ------------------------------------------------------------------------------------------------------
    12. t2 内存地址:31874198304,十六进制位:76bd9ab20,二进制位:11101101011110110011010101100100000
    13. 线程t2获得锁obj4
    14. 偏向锁延迟生效后,线程t2 打印obj5 对象偏向锁状态,其对象头markword标记:
    15. java.lang.Object object internals:
    16. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    17.       0     4        (object header)                           05 c8 aa dd (00000101 11001000 10101010 11011101) (-576010235)
    18.       4     4        (object header)                           c4 7f 00 00 (11000100 01111111 00000000 00000000) (32708)
    19.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    20.      12     4        (loss due to the next object alignment)
    21. Instance size: 16 bytes
    22. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    23. t2 id:14,二进制位:1110
    24. ------------------------------------------------------------------------------------------------------
    25. [线程t3] 获得锁obj5
    26. 偏向锁延迟生效后,偏向锁状态-膨胀为:重量级锁 ,其对象头markword标记:
    27. java.lang.Object object internals:
    28. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    29.       0     4        (object header)                           7a 0c 81 dc (01111010 00001100 10000001 11011100) (-595522438)
    30.       4     4        (object header)                           c4 7f 00 00 (11000100 01111111 00000000 00000000) (32708)
    31.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    32.      12     4        (loss due to the next object alignment)
    33. Instance size: 16 bytes
    34. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    35. [线程t4] 获得锁obj5
    36. 偏向锁延迟生效后,偏向锁状态-膨胀为:重量级锁 ,其对象头markword标记:
    37. java.lang.Object object internals:
    38. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    39.       0     4        (object header)                           7a 0c 81 dc (01111010 00001100 10000001 11011100) (-595522438)
    40.       4     4        (object header)                           c4 7f 00 00 (11000100 01111111 00000000 00000000) (32708)
    41.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    42.      12     4        (loss due to the next object alignment)
    43. Instance size: 16 bytes
    44. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    45. ################################################################################################################################################################################
    46. ##########################################################################情况二############################################################################################
    47. JVM偏向锁已经生效:偏向锁状态下的对象头markword标记:
    48. java.lang.Object object internals:
    49. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    50.       0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
    51.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    52.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    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. t2 内存地址:31874198752,十六进制位:76bd9ace0,二进制位:11101101011110110011010110011100000
    58. 线程t2获得锁obj4
    59. 偏向锁延迟生效后,线程t2 打印obj5 对象偏向锁状态,其对象头markword标记:
    60. java.lang.Object object internals:
    61. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    62.       0     4        (object header)                           05 00 91 86 (00000101 00000000 10010001 10000110) (-2037317627)
    63.       4     4        (object header)                           ed 7f 00 00 (11101101 01111111 00000000 00000000) (32749)
    64.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    65.      12     4        (loss due to the next object alignment)
    66. Instance size: 16 bytes
    67. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    68. t2 id:14,二进制位:1110
    69. ------------------------------------------------------------------------------------------------------
    70. [线程t3] 获得锁obj5
    71. 偏向锁延迟生效后,偏向锁状态-保持:偏向锁 ,其对象头markword标记:
    72. java.lang.Object object internals:
    73. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    74.       0     4        (object header)                           05 00 91 86 (00000101 00000000 10010001 10000110) (-2037317627)
    75.       4     4        (object header)                           ed 7f 00 00 (11101101 01111111 00000000 00000000) (32749)
    76.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    77.      12     4        (loss due to the next object alignment)
    78. Instance size: 16 bytes
    79. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    80. [线程t4] 获得锁obj5
    81. 偏向锁延迟生效后,偏向锁状态-膨胀为:重量级锁 ,其对象头markword标记:
    82. java.lang.Object object internals:
    83. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    84.       0     4        (object header)                           0a 9c 80 88 (00001010 10011100 10000000 10001000) (-2004837366)
    85.       4     4        (object header)                           ed 7f 00 00 (11101101 01111111 00000000 00000000) (32749)
    86.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    87.      12     4        (loss due to the next object alignment)
    88. Instance size: 16 bytes
    89. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    90. ################################################################################################################################################################################
    91. ##########################################################################情况三############################################################################################
    92. JVM偏向锁已经生效:偏向锁状态下的对象头markword标记:
    93. java.lang.Object object internals:
    94. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    95.       0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
    96.       4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
    97.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    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. t2 内存地址:31874198880,十六进制位:76bd9ad60,二进制位:11101101011110110011010110101100000
    103. 线程t2获得锁obj4
    104. 偏向锁延迟生效后,线程t2 打印obj5 对象偏向锁状态,其对象头markword标记:
    105. java.lang.Object object internals:
    106. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    107.       0     4        (object header)                           05 f8 8e cd (00000101 11111000 10001110 11001101) (-846268411)
    108.       4     4        (object header)                           c9 7f 00 00 (11001001 01111111 00000000 00000000) (32713)
    109.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    110.      12     4        (loss due to the next object alignment)
    111. Instance size: 16 bytes
    112. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    113. t2 id:14,二进制位:1110
    114. ------------------------------------------------------------------------------------------------------
    115. [线程t3] 获得锁obj5
    116. 偏向锁延迟生效后,偏向锁状态-膨胀为:轻量级锁 ,其对象头markword标记:
    117. java.lang.Object object internals:
    118. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    119.       0     4        (object header)                           08 09 8d 0e (00001000 00001001 10001101 00001110) (244123912)
    120.       4     4        (object header)                           c9 7f 00 00 (11001001 01111111 00000000 00000000) (32713)
    121.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    122.      12     4        (loss due to the next object alignment)
    123. Instance size: 16 bytes
    124. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    125. [线程t4] 获得锁obj5
    126. 偏向锁延迟生效后,偏向(轻量)锁状态-膨胀为:重量级锁 ,其对象头markword标记:
    127. java.lang.Object object internals:
    128. OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
    129.       0     4        (object header)                           0a 2c 01 cc (00001010 00101100 00000001 11001100) (-872338422)
    130.       4     4        (object header)                           c9 7f 00 00 (11001001 01111111 00000000 00000000) (32713)
    131.       8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
    132.      12     4        (loss due to the next object alignment)
    133. Instance size: 16 bytes
    134. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    135. ################################################################################################################################################################################
  • 相关阅读:
    【python】(十)python的错误与异常
    525. 连续数组 (前缀和 + 哈希)
    【20220121】Voice conversion
    Redis 源码简洁剖析 13 - RDB 文件
    Java 数字金额,字符串格式化
    第九章 APP项目测试(此章完结)
    vscode远程连接开发机失败/解决方案大合集
    LLM 05-大模型法律
    Centos 安装MySQL 5.7.38
    Scrum Master的技能和经验要求
  • 原文地址:https://blog.csdn.net/PYXLY1314/article/details/126021584