0

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).

Soumya
  • 1,350
  • 3
  • 19
  • 34
  • The diagram displayed in the question can give a visual understanding more as what have been explained in the questions below. http://stackoverflow.com/questions/16264118/how-jvm-stack-heap-and-threads-are-mapped-to-physical-memory-or-operation-syste – Soumya Oct 22 '15 at 14:47

2 Answers2

1

addTwoNumbers: all the variables are local variable. They will be stored in the stack, when this method is called. Because each method creates its own stack. So the two threads will be having, two completely different stacks. So they are safe from multi threading perspective. You don't need any lock or mutex.

If you were using any objects (reference variable) which are always stored in the Heap Memory area. So would need the synchronization.

Also you might need the synchronization, when you are updated the state of the singleton object. Because that is shared among the threads.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
  • So even if they are calling a singleton class, these two threads will have their own respective stacks wherein the local variables will be created. But lets say, in the above example, the function is static and they use static class variables (not local variables), then will in that case synchronization be required? – Soumya Oct 22 '15 at 04:08
  • Yes. Because the static variable are loaded once per class loader. Only one copy of the variable will exist and shared by the threads. – YoungHobbit Oct 22 '15 at 04:25
  • Thanks for the explanation. The heap memory and per thread stack differentiation is very significant in here for the understanding of the race condition. – Soumya Oct 22 '15 at 14:39
1

No, it is never possible for two or more threads to have access to the same local variables in a method.

Note though that when the variables are of reference type (they are not in your example, you are only using the primitive type int) then the objects that they point to may be accessed by different threads at the same time.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Ok, so if the function assigns the values to a class variable instead of local variables, then will there be a race condition (two threads accessing the same variables as the class is a singleton)? – Soumya Oct 22 '15 at 04:10
  • Yes, with your example and multiple threads, if you used instance variables or static variables, you would certainly see some race conditions because you have unprotected access from multiple threads to the same variables. – Erwin Bolwidt Oct 22 '15 at 05:17
  • Perfect! Thank you for the explanation. – Soumya Oct 22 '15 at 14:38