P
Philipp
Hello,
By searching the web I found lots of references about the below
question. At the same time, I couldn't find an *authoritative*
references (eg. by Goetz, Lea or Bloch) about it. Please refer me to the
correct web site or book. Thanks (I'm using JVM 1.3, so the "old" memory
model)
The question: Are the following code snippets thread safe and why? (my
opinion is on top, please correct if wrong):
// Not thread safe. "value" needs to be volatile, else another thread
// might see a stale value
public final class Test1 {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
// Not thread safe. Because assignment to long is not atomic (although
// volatile).
public final class Test2 {
private volatile long value = 0;
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
}
// Thread safe. Because reference assignment is atomic
// Question: Has immutability of String any influence here?
// Question: Could "newValue" be incompletely constructed?
public final class Test3 {
private volatile String value = "Hello";
public String getValue() {
return value;
}
public void setValue(String newValue) {
this.value = newValue;
}
}
// Not thread safe. Because construction may not have finished when
// reference assignment is made (although volatile).
public final class Test4 {
private volatile Date value = new Date();
public Date getValue() {
return value;
}
public void makeNewDate() {
this.value = new Date();
}
}
// (Still) not thread safe. Because 2 step assignement makes no
// guaranteeing that the ref will not be assigned earlier.
public final class Test5 {
private volatile Date value = new Date();
public Date getValue() {
return value;
}
public void makeNewDate() {
Date d = new Date();
this.value = d;
}
}
Thanks for your answers and clarifications.
Phil
By searching the web I found lots of references about the below
question. At the same time, I couldn't find an *authoritative*
references (eg. by Goetz, Lea or Bloch) about it. Please refer me to the
correct web site or book. Thanks (I'm using JVM 1.3, so the "old" memory
model)
The question: Are the following code snippets thread safe and why? (my
opinion is on top, please correct if wrong):
// Not thread safe. "value" needs to be volatile, else another thread
// might see a stale value
public final class Test1 {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
// Not thread safe. Because assignment to long is not atomic (although
// volatile).
public final class Test2 {
private volatile long value = 0;
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
}
// Thread safe. Because reference assignment is atomic
// Question: Has immutability of String any influence here?
// Question: Could "newValue" be incompletely constructed?
public final class Test3 {
private volatile String value = "Hello";
public String getValue() {
return value;
}
public void setValue(String newValue) {
this.value = newValue;
}
}
// Not thread safe. Because construction may not have finished when
// reference assignment is made (although volatile).
public final class Test4 {
private volatile Date value = new Date();
public Date getValue() {
return value;
}
public void makeNewDate() {
this.value = new Date();
}
}
// (Still) not thread safe. Because 2 step assignement makes no
// guaranteeing that the ref will not be assigned earlier.
public final class Test5 {
private volatile Date value = new Date();
public Date getValue() {
return value;
}
public void makeNewDate() {
Date d = new Date();
this.value = d;
}
}
Thanks for your answers and clarifications.
Phil