I have a class of common code that is thread safe.
One of the methods in that class is abstract and needs to be overridden for different implementations.
I need to ensure or at least flag to other developers that all implementations of this method need to be thread-safe.
What is the best way to do this?
Is there a keyword or annotation to this effect?
I have already tried abstract synchronized
but that combination of keywords is not allowed.
This link to the JLS confirms that we can't mix abstract and synchronized.
Though much weaker than a keyword or standard annotation, but stronger than documentation: perhaps try a Marker interface?
This is a stretch, but might help, in that the derived class makes a declaration (edit: new example tests the declaration):
You can't do it directly. One thing you can do is have the method be concrete, but invoke an abstract method:
That way, doFoo() will always* be invoked under the synchronization established by foo().
* unless someone invokes it directly, so you should name and document it to make it clear that they shouldn't.
From Synchronized method in subclass
You can also have a look at, A synchronized method in the superclass acquires the same lock as one in the subclass.