val = false
if(!val) {
val = true;
}
Suppose say there are two threads A and B or even more....
Always the Interviewning time between two threads could be extremely less..probably depending upon the RAM speed of the server or our computer...
So If Thread A checks for the value of false...and before setting it as true..Thread B would access
the same if loop with the value set as false only...
So that's why we need to go for synchronized method so that only one thread can access that block at a time.
Synchronized method() {
val = false
if(!val) {
val = true;
}