abstract class A {
protected abstract int isRunning();
public void concreteMethod() {
synchroinzed(isRunning()) {
//do stuff
}
}
}
class B extends A {
int running_ = 0;
public int isRunning() {
return running_;
}
}
The problem I get: int is not a valid type’s argument for the synchronized statement. How do I do this?
If you want to prevent simultaneous execution of your block and the method
isRunning(), you can’t do it exactly as you want becausesynchronizationcan’t be inherited (only an implementation can justifysynchronization).Here’s the nearest you can do :
If you can afford to lock the entire
concreteMethod()and not just a block, you have the simple solution to add thesynchronizedkeyword to theconcreteMethodandisRunning():