G
gubd2005
Hi folks.
I have a question about the example code at the bottom of this message
(excuse the omission of getters/setters).
Basically I have method in the class Test that starts a new thread (an
instance of the class X). Lets say thread #1 is the thread that calls
the method in Test, and thread #2 is the newly created thread. Thread
#1 waits until thread #2 reaches a checkpoint. Thread #2, meanwhile,
writes a value (writeValue) and then notifies thread #1 that it has
reached the checkpoint. Thread #1, after passing the checkpoint, tries
outputting the writeValue that thread #2 wrote.
Here's my question: Is it necessary to put the write of writeValue in
class X, and the read of writeValue in class Test, into synchronized
blocks? The discussion in the 2nd edition of the Java Language Spec
with regards to thread working memory and synchronization implies that
the answer is YES, they must be surrounded by synchronized blocks.
Otherwise, writeValue written by thread #2 can remain in its working
memory, and thread #1 may only see the initial value of writeValue.
This is despite the guarantee that the "checkpoint" provides that
thread #2 will have written writeValue before thread #1 tries reading
it.
Another point of confusion for me is the disappearance of the
discussion of thread working memory from the 3rd edition of the Java
Language Spec. I haven't gone through the details of the 3rd edition
spec, but is the concept of working memory still there, just disguised
in different terms? A virtually identical discussion of thread working
memory is in the latest edition of the VM spec (2nd edition), so was
the omission just to clean up the duplication of the discussion between
the two specs? Or was there another reason?
Thanks.
Bob.
public class X extends Thread {
public boolean checkPoint = false;
public int writeValue = 0;
public void run() {
// should the following line be in a synchronized block?
writeValue = 500;
synchronized (this) {
checkPoint = true;
this.notify();
}
for(; { // do some stuff forever without the thread ever dying }
}
}
public class Test {
public void m() throws InterruptedException {
X x = new X();
x.start();
synchronized(x) {
if (!x.checkPoint) {
x.wait();
}
}
// should the following line be in a synchronized block?
System.out.println("x.writeValue == "+x.writeValue);
}
}
I have a question about the example code at the bottom of this message
(excuse the omission of getters/setters).
Basically I have method in the class Test that starts a new thread (an
instance of the class X). Lets say thread #1 is the thread that calls
the method in Test, and thread #2 is the newly created thread. Thread
#1 waits until thread #2 reaches a checkpoint. Thread #2, meanwhile,
writes a value (writeValue) and then notifies thread #1 that it has
reached the checkpoint. Thread #1, after passing the checkpoint, tries
outputting the writeValue that thread #2 wrote.
Here's my question: Is it necessary to put the write of writeValue in
class X, and the read of writeValue in class Test, into synchronized
blocks? The discussion in the 2nd edition of the Java Language Spec
with regards to thread working memory and synchronization implies that
the answer is YES, they must be surrounded by synchronized blocks.
Otherwise, writeValue written by thread #2 can remain in its working
memory, and thread #1 may only see the initial value of writeValue.
This is despite the guarantee that the "checkpoint" provides that
thread #2 will have written writeValue before thread #1 tries reading
it.
Another point of confusion for me is the disappearance of the
discussion of thread working memory from the 3rd edition of the Java
Language Spec. I haven't gone through the details of the 3rd edition
spec, but is the concept of working memory still there, just disguised
in different terms? A virtually identical discussion of thread working
memory is in the latest edition of the VM spec (2nd edition), so was
the omission just to clean up the duplication of the discussion between
the two specs? Or was there another reason?
Thanks.
Bob.
public class X extends Thread {
public boolean checkPoint = false;
public int writeValue = 0;
public void run() {
// should the following line be in a synchronized block?
writeValue = 500;
synchronized (this) {
checkPoint = true;
this.notify();
}
for(; { // do some stuff forever without the thread ever dying }
}
}
public class Test {
public void m() throws InterruptedException {
X x = new X();
x.start();
synchronized(x) {
if (!x.checkPoint) {
x.wait();
}
}
// should the following line be in a synchronized block?
System.out.println("x.writeValue == "+x.writeValue);
}
}