I was pondering over this question today. Here is a scenario which I am thinking.
Singleton Class with a method
public int addTwoNumbers(int a, int b) {
int x = a;
int y = b;
int z = a + b;
return z;
}
Thread - 1
singletonClassObj.addTwoNumbers(10, 20);
Thread - 2
singletonClassObj.addTwoNumbers(100, 200);
Now my question is what, let say Thread-1 gets executed first and calls the method. So is it possible that before the entire function gets executed by thread - 1, thread -2 calls the function and alters the values of x and y? For example, Thread -1, sends the data as 10 and 20 and before assigning the summation to variable z, thread -2 changes the values of x and y to 100 and 200, which in-turn makes thread-1 to return 300 instead of 30. To overcome this, we need to put in lock or mutex, but is this possible (without the mutex).