Hello!
This question might appear very simple, but i haven't found an answer yet, so i'm asking the stack overflow community. As the title suggests i have a Class with several getXXX() methods where some of them may return null. This is documented and the user of this class should be aware of this fact.
To simplify the usage of this class, i had the idea to add some handy hasXXX() methods which indicates if specific fields are set or not. First, this seems to be a good idea... but then Thread Safety comes to mind.
Since instances of this class might be shared across threads the values of the properties might change. As we all know check-then-act is only possible if we know that the state will not change after the check-method has been invoked, even if we're interrupted while doing our check-then-act stuff.
The following solutions came to my mind:
- Supply the user of this class a way to "lock" the instance for state changes while doing check-then-act code.
- Remove the hasXXX() methods since they are useless for mutable classes.
I dont find this as a rare case and some SO members might have stubled upon this issue before and found a solution...
Foobaerchen