If you want to prevent simultaneous execution of your block and the method isRunning()
, you can't do it exactly as you want because synchronization
can't be inherited (only an implementation can justify synchronization
).
Here's the nearest you can do :
class A {
protected Object lock = new Object();
protected abstract int isRunning();
public void concreteMethod() {
synchronized(lock) {
//do stuff
}
}
}
class B extends A {
int running_ = 0;
public int isRunning() {
synchronized(lock) {
return running_;
}
}
}
If you can afford to lock the entire concreteMethod()
and not just a block, you have the simple solution to add the synchronized
keyword to the concreteMethod
and isRunning()
:
class A {
protected abstract int isRunning();
public synchronized void concreteMethod() {
//do stuff
}
}
class B extends A {
int running_ = 0;
public synchronized int isRunning() {
return running_;
}
}