Daniel John Debrunner <[EMAIL PROTECTED]> writes:
> Knut Anders Hatlen wrote:
>> They use a pattern of first copying the instance
>> variables into local variables, then do the work on the local
>> variables, and finally write back to the instance variables. While I
>> find that a little confusing because I would expect the run-time
>> compiler to be able to perform that kind of optimization itself,
>> that's a different issue from what is discussed in this thread.
>
> Can the run-time optimizer/compiler perform such an optimization when
> the instance variable is not final? I would have thought not.
Yes, I believe it can. Java's memory model basically allows the
compiler to assume that the variables are accessed by a single thread
as long as it doesn't hit a memory barrier (like a volatile variable
or a synchronized block).
The standard example I have seen, is that the JVM is allowed to
rewrite this code
while (running) {
sleep();
}
to this
if (running) {
while (true) {
sleep();
}
}
as long as 'running' is not a volatile variable. I'm sure I've read
somewhere that the server VM actually will do this optimization, but I
can't find a reference to it right now.
--
Knut Anders