Thread safety and atomic assignment (again)



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
.



Relevant Pages

  • C# threading question
    ... public class FileSystemMonitor ... private volatile bool _bStopThreads; ... public void ReqThreadStop() ... FileSystemMonitor watcher; ...
    (microsoft.public.dotnet.csharp.general)
  • Re: Best way to create an array of Strings in a called method
    ... > started using Java. ... > references to the class. ... > public void setStringArray{ ... > Strings in a called method. ...
    (comp.lang.java.programmer)
  • Re: a question about inheritance
    ... kevin scribbled the following: ... > public void actionPerformed ... every other class is a subclass of Object, ... have references of various class types referring to it. ...
    (comp.lang.java.programmer)
  • Re: Modifying cell references
    ... Try recording a macro when you do it manually. ... "Sandeep" wrote: ... references as the parameters to the same function are different. ... everything before getValue(, and keeps everything as it is after getValue(. ...
    (microsoft.public.excel.misc)
  • Re: newbie question on returning an object
    ... public void someMethod() { ... //now there are 2 refs to a and 2 refs to a ... local a and b references will be destroyed when returning from the method. ... public void otherMethod() { ...
    (comp.lang.java.help)