(实践)一文搞定synchronized锁升级过程

一文搞定synchronized锁升级过程

  • 一、无锁状态
  • 二、偏向锁

        <dependency>
            <groupId>org.openjdk.jolgroupId>
            <artifactId>jol-coreartifactId>
            <version>0.16version>
        dependency>

一、无锁状态

public class MySynchronizedLock {

    public static void main(String[] args) {
        String s = ClassLayout.parseInstance(new MySynchronizedLock()).toPrintable();
        System.out.println(s);
    }
}

结果:

OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
  8   4        (object header: class)    0xf800c105
 12   4        (object alignment gap)    
Instance size: 16 bytes

二、偏向锁

public class MySynchronizedLock {


    public static void main(String[] args) {
        Object obj = new Object();

        // 1. 无锁状态
        System.out.println("无锁状态");
        System.out.println(ClassLayout.parseInstance(obj).toPrintable());

        // 2. 偏向锁状态
        synchronized (obj) {
            System.out.println("偏向锁状态");
            System.out.println(ClassLayout.parseInstance(obj).toPrintable());
        }

        // 3. 轻量级锁状态
        new Thread(() -> {
            synchronized (obj) {
                System.out.println("轻量级锁状态 - 线程1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(ClassLayout.parseInstance(obj).toPrintable());
            }
        }).start();

        // 4. 重量级锁状态
        new Thread(() -> {
            synchronized (obj) {
                System.out.println("重量级锁状态 - 线程2");
                System.out.println(ClassLayout.parseInstance(obj).toPrintable());
            }
        }).start();
    }

}

结果:

无锁状态
# 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
java.lang.Object object internals:
OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
  8   4        (object header: class)    0xf80001e5
 12   4        (object alignment gap)    
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

偏向锁状态
java.lang.Object object internals:
OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     0x0000700003fe39d0 (thin lock: 0x0000700003fe39d0)
  8   4        (object header: class)    0xf80001e5
 12   4        (object alignment gap)    
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

轻量级锁状态 - 线程1
java.lang.Object object internals:
OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     0x00007fe95f021eba (fat lock: 0x00007fe95f021eba)
  8   4        (object header: class)    0xf80001e5
 12   4        (object alignment gap)    
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

重量级锁状态 - 线程2
java.lang.Object object internals:
OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     0x00007fe95f021eba (fat lock: 0x00007fe95f021eba)
  8   4        (object header: class)    0xf80001e5
 12   4        (object alignment gap)    
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

你可能感兴趣的:(java,开发语言)