If you have the following class... public class A { int m_i = 10; public void method1() { int x = 0; x = 10; } } Does method1() need to be synchronized even though it does not access any class instance variables? Asked another way: Do 2 or more threads accessing the same un-synchronized method (method1() in this case) step on eachother's toes, even if the method only uses variables local to that method? I hope I've explained it well. Thanks, Kyle
No you don't have to synchronized the method. Every local variables within the method are local, and during execution they have their own memory space. So different threads execution have their own stack of local variables.
As best I understand it, you would not need to synchronize this. Each invocation of method1() will have its own frame on the stack, which x will be contained/scoped by. However, if you were changing m_i even once in the method, you would of course need to synchronize.
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, that will fit in a stocking