java 锁之synchronized

synchronized

互斥 可重入

案例

    /**
     * 修饰非静态方式
     */
    public synchronized void synchronizedMethod(){
        System.out.println("synchronized 非静态方法锁");
    }
  
     /**
     * 对应指令码
     */
  public synchronized void synchronizedMethod();
    descriptor: ()V
    **flags: ACC_PUBLIC, ACC_SYNCHRONIZED**
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String synchronized 非静态方法锁
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 7: 0
        line 8: 8
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  this   Lcom/javabase/lock/sync/SynchronizedDemo;

    /**
     * 修饰静态方式
     */
    public synchronized static void  synchronizedStaticMethod(){
        System.out.println("synchronized 静态方法锁");
    }
    
    /**
     * 对应指令码
     */
  public static synchronized void synchronizedStaticMethod();
    descriptor: ()V
    **flags: ACC_PUBLIC, ACC_STATIC, ACC_SYNCHRONIZED**
    Code:
      stack=2, locals=0, args_size=0
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #5                  // String synchronized 静态方法锁
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 11: 0
        line 12: 8

    /**
     * 修饰块(this)
     */
    public void  synchronizedObjectStatic(){
        synchronized(this){
            System.out.println("synchronized 块锁--对象的方式");
        }
    }
    
    /**
     * 对应指令码
     */
   public void synchronizedObjectStatic();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=1
         0: aload_0
         1: dup
         2: astore_1
         **3: monitorenter**
         4: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         7: ldc           #6                  // String synchronized 块锁--对象的方式
         9: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        12: aload_1
        **13: monitorexit**
        14: goto          22
        17: astore_2
        18: aload_1
        **19: monitorexit**
        20: aload_2
        21: athrow
        22: return
      Exception table:
         from    to  target type
             4    14    17   any
            17    20    17   any
      LineNumberTable:
        line 24: 0
        line 25: 4
        line 26: 12
        line 27: 22
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      23     0  this   Lcom/javabase/lock/sync/SynchronizedDemo;
      StackMapTable: number_of_entries = 2
        frame_type = 255 /* full_frame */
          offset_delta = 17
          locals = [ class com/javabase/lock/sync/SynchronizedDemo, class java/lang/Object ]
          stack = [ class java/lang/Throwable ]
        frame_type = 250 /* chop */
          offset_delta = 4

    /**
     * 修饰块(Class)
     */
    public static void synchronizedClassStatic(){
        synchronized(SynchronizedDemo.class){
            System.out.println("synchronized 块锁--类的方式");
        }
    }
    
    /**
     * 对应指令码
     */
    public static void synchronizedClassStatic();
    descriptor: ()V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=0
         0: ldc           #7                  // class com/javabase/lock/sync/SynchronizedDemo
         2: dup
         3: astore_0
         **4: monitorenter**
         5: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         8: ldc           #8                  // String synchronized 块锁--类的方式
        10: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        13: aload_0
        **14: monitorexit**
        15: goto          23
        18: astore_1
        19: aload_0
        **20: monitorexit**
        21: aload_1
        22: athrow
        23: return
      Exception table:
         from    to  target type
             5    15    18   any
            18    21    18   any
      LineNumberTable:
        line 33: 0
        line 34: 5
        line 35: 13
        line 36: 23
      StackMapTable: number_of_entries = 2
        frame_type = 255 /* full_frame */
          offset_delta = 18
          locals = [ class java/lang/Object ]
          stack = [ class java/lang/Throwable ]
        frame_type = 250 /* chop */
          offset_delta = 4


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值