java的guardedBy注解

what:

  常见类型:

  1、@GuardedBy( "this" ) 受 对象内部锁保护
  2、@GuardedBy( "fieldName" ) 受 与fieldName引用相关联的锁 保护。
  3、@GuardedBy( "ClassName.fieldName" ) 受 一个类的静态field的锁 保存。
  4、@GuardedBy( "methodName()" ) 锁对象methodName() 方法的返值,受这个锁保护。
  5、@GuardedBy( "ClassName.class" ) 受 ClassName类的直接锁对象保护。而不是这个类的某个实例的锁对象。

 

How:

  @GuardedBy( "this" ) :  

 

    bitmapPreFiller需要在synchronized 锁类对象级别的代码块中,如下:

 

    synchronized锁成员方法,属于synchronized 锁类对象级别 

 

  GuardedBy( "fieldName" ) :

 

     使用managers对象时,需要将managers在synchronized锁managers对象的代码块中,如:

 

 

 

     当有人获得了凭据的同步锁定时,可以访问金额,因此,BankAccount中的金额由凭据保护。

 

posted @ 2022-05-29 16:58  修心而结网  阅读(2194)  评论(0编辑  收藏  举报