I have a method as follows:
public static void method() {
int i = 0;
i = i + 1;
}
I have an int variable inside a static method. And the method is accessed by several Threads.
My questions are:
i
variable go to race condition?If the variable is declared within the method, then it lives in the stackframe provided for a single invocation of the method. The stackframe is accessed only by the thread invoking the method. There is no race condition in the posted example, every invocation of the method gets its own copy of the variable. You need shared state in order to have a race condition.
These stackframes are the things that pile up when executing a recursive method, and take up stack space until at some point the system runs out of space and a stackoverflow error occurs, because the recursion results in more and more stackframes getting allocated, while none of the method calls get a chance to complete (which would free up their stack space).