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.
}
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.
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;
}
Thanks for any suggestion.
Lawrence