通过同步可以安全地访问此变量吗? [英] Is this variable being safely accessed by using synchronization?

查看:131
本文介绍了通过同步可以安全地访问此变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的案例中,Java类具有包含同步块的超类.

I have a case in which a Java class has a superclass that contains a synchronized block.

Class SuperClassA {
     private Bitmap bmpA;

     protected abstract Bitmap createBitmap();

     public void run() {
          synchronized (this) {
               bmpA = createBitmap();
          }
     }

     // some other codes.
}

Class SubClassB extends SuperClassA {
     private Bitmap outBmpB;

     protected Bitmap createBitmap() {
          outBmpB = ..... // create and process "outBmpB".

          Bitmap bmp;
          bmp = ..... // create and process "bmp".
          return bmp;
     }

     public Bitmap getOutBmpB() {
          Bitmap tempBmp;
          synchronized (this) {
               tempBmp = outBmpB.clone();
          }
          return tempBmp;
     }

     // some other codes.
}

B类中的"getOutBmpB()"方法由一个线程运行,而B类中继承的"run()"方法由另一个线程运行.在ClassB中实现的"createBitmap()"方法应在"run()"方法内的同步块中运行.

The "getOutBmpB()" method in Class B is run by a thread while the inherited "run()" method in ClassB is run by another thread. The "createBitmap()" method implemented in ClassB should be run in a synchronized block inside the "run()" method.

我的问题是,我不确定两个线程是否安全地访问了ClassB中新定义的类变量"outBmpB".我不确定"run()"方法中的"synchronized(this)"块是否也会锁定"仅在ClassB中定义的"outBmpB"变量?如果没有,那么我可以在"createBitmap()"实现中添加"synchronized(this)"块.例如

My question is that I am not sure whether the newly defined class variable "outBmpB" in ClassB is safely accessed by the two threads. I am not sure the "synchronized (this)" block in the "run()" method would also "lock" the "outBmpB" variable defined just in ClassB ? If not, then could I add a "synchronized (this)" block in the "createBitmap()" implementation. e.g.

 protected Bitmap createBitmap() {
      synchronized (this) {
           outBmpB = ..... // create and process "outBmpB".
      }

      Bitmap bmp;
      bmp = ..... // create and process "bmp".
      return bmp;
 }

感谢您的任何建议.

劳伦斯

推荐答案

您不必 对其进行同步,因为它已经在超类中进行了同步,并且没有其他调用,但是您应该. createBitmap()的实现依赖于超类的实现细节. 在访问共享字段的每一点都要进行同步.

You don't have to synchronize it since it is already synchronized in super class and there are no other calls, but you should. Your implementation of createBitmap() relies on implementation details from the super class. Do synchronization at every point you access your shared fields.

由于这个事实,您当前的子类代码很容易出错!

Due to this facts your current subclass' code is very error-prone!

尽管在this上进行同步还是有问题的,但在其他人无法访问的私有对象上进行同步是一个更好的主意.因此,客户端使用您的类对象无法破坏您的代码并对其进行同步.

While it is questionable to ever synchronize over this, it is a much better idea to synchronize over a private object nobody else can access. So your code cannot be broken by clients using your class objects and synchronize on it.

这篇关于通过同步可以安全地访问此变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆