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++;
}
}