The only issue with isEmpty() being synchronized is that you don't know what happens below. Although your reasoning is reasonable, it assumes that the underlying Stack also behaves reasonably. Most likely, this is the case, but you cannot rely on it as a whole.
And the second part of your question, there is nothing wrong with blocking pop operations, see this for the full implementation of all possible strategies.
And one more suggestion: if you create a class that can be reused in several parts of the application (or even several applications), do not use synchronized methods. Do this instead:
public class Whatever { private Object lock = new Object(); public void doSomething() { synchronized( lock ) { ... } } }
The reason for this is that you really donβt know whether users of your class want to synchronize your Whatever instances or not. If they do, they can interfere with the class itself. Thus, you have your own private castle, which no one can interfere with.
biziclop
source share