Re: Local variables in Java
John W. Kennedy wrote:
ankur wrote:
So I was wondering how local variables are handled in Java. If the
same method is called upon many objects ( of same type) I guess the
method calls happen one after the another so that for each method call
new local variables get defined , but at any point in time there is
only one copy of all local variables defined ( while method calls are
happening). Correct ???
But then how would this work in a multicore system ( more than one
processor, same harddrive,same RAM) system if same method call is
executed by same JVM in parallel on the two cores for two different
objects ( of same class) ?
Cores have nothing to do with it; threads do.
Cores do have an influence, at least in the transition from one to at least two.
Threads tend to be "synchronized by default" in a uniprocessor environment.
Some, and I emphasize only some, of concurrency's behind-biters show up with
more probability only when multiple cores are involved.
The reason is the memory model. Concurrency isn't just about race conditions
and deadlocks and the like, it's about when changes incurred in one thread are
visible in another. The rules for this are called Java's "memory model".
In multi-core systems, each thread likely operates with a thread-local memory
that lives entirely on the CPU's local cache or local memory. Without
synchronization, the system might not share memory changes across CPUs.
In a single-processor environment, multiple threads could "accidentally" share
the same copy of the data even if we forget to synchronize properly. I have
read that Java programs that evinced no threading issues in uniprocessor
environments experienced them when ported to multi-core platforms.
Note that even if such programs are run on uniprocessor systems without
apparent problems, that does not make them correct.
--
Lew