I have one thread that runs periodically every 5 minutes, it may change the value of a flag and many threads reads that flag. Please suggest if the following design has any issues. I choose volatile so every thread gets correct value of flag and no caching. I do not see AtomicBoolean makes much sense here.
Class A {
private static volatile boolean flag = false;
//Calls every 5 minutes
//Value of this flag may change every 5 minutes by thread.
checkAndSetFlag(boolean newFlag) {
flag= newFlag;
}
}
A number of threads will just check the value as follows. I have no issue if some threads reads the old value at the same time the value is changing.
Class B {
doSomething() {
if(A.flag) {
doSomethingElse();
}
}
}