Question about Master and Working Memory from Chapter 17 of the Java Language Specification  
Author Message
SSMelaNB





PostPosted: 2007-4-3 1:59:00 Top

java-programmer, Question about Master and Working Memory from Chapter 17 of the Java Language Specification Ok the following is from Chapter 17 of the Java Language
Specification.

"A variable is any location within a program that may be stored into.
This includes not only class variables and instance variables but also
components of arrays. Variables are kept in a main memory that is
shared by all threads. Because it is impossible for one thread to
access parameters or local variables of another thread, it doesn't
matter whether parameters and local variables are thought of as
residing in the shared main memory or in the working memory of the
thread that owns them.

Every thread has a working memory in which it keeps its own working
copy of variables that it must use or assign. As the thread executes a
program, it operates on these working copies. The main memory contains
the master copy of every variable. There are rules about when a thread
is permitted or required to transfer the contents of its working copy
of a variable into the master copy or vice versa."

Does this mean that, say, in the following code, there are two working
copies of the integer variable X (one belonging to aThread and one
belonging to bThread) and one master copy?

public class TestThread {
public static void main(String[] args) {
aClass a = new aClass();
Thread aThread = new Thread( a );
Thread bThread = new Thread( a );
aThread.start();
bThread.start();
}
}


class aClass implements Runnable{
int X;
public void run( ) {
while (true) {update();}
}
synchronized void update(){
X++;
}
}

 
Tom Hawtin





PostPosted: 2007-4-3 2:24:00 Top

java-programmer >> Question about Master and Working Memory from Chapter 17 of the Java Language Specification email***@***.com wrote:
> Ok the following is from Chapter 17 of the Java Language
> Specification.
>
> "A variable is any location within a program that may be stored into.

I spent some time looking for that phrase. Turns out that it's in the
second edition JLS (start of 17.1).

I should have noticed earlier because the is referring to the old memory
model that never actually worked. Don't waste your time on the second
edition, particularly for that chapter. Get hold of a third edition JLS.
There is a copy on java.sun.com.

Tom Hawtin
 
Patricia Shanahan





PostPosted: 2007-4-3 3:12:00 Top

java-programmer >> Question about Master and Working Memory from Chapter 17 of the Java Language Specification Tom Hawtin wrote:
> email***@***.com wrote:
>> Ok the following is from Chapter 17 of the Java Language
>> Specification.
>>
>> "A variable is any location within a program that may be stored into.
>
> I spent some time looking for that phrase. Turns out that it's in the
> second edition JLS (start of 17.1).
>
> I should have noticed earlier because the is referring to the old memory
> model that never actually worked. Don't waste your time on the second
> edition, particularly for that chapter. Get hold of a third edition JLS.
> There is a copy on java.sun.com.

http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html

Patricia