In Java which code is shared between multiple threads and which code is being copied to be used for one particular thread ?
Too be more precise if one would consider the following fragment
public void foo () {
int a = 0;
a++;
}
being executed by multiple threads concurrently will each thread hold its own copy of the variable a
or is it shared between the threads?
No, a
is a local variable. That means each invocation of foo
gets its own separate variable - whether that's in multiple threads or even recursively within the same thread.
Note that this isn't about sharing code - it's about sharing data. The same code will be running (well, probably) in all threads, but each invocation will work with a separate set of local variables.
See more on this question at Stackoverflow