Skip to main content

Posts

Showing posts from March, 2007

Safe Construction and the JMM

Another question from a viewer. We have this code: class C {   private int x;   public C(int v) {     this.x = v;   }   public synchronized int get() {     return this.x;   }   public synchronized void set(int v) {     this.x = v;   } } (For the actual question as it was worded, the short answer is, "yes") The question was one I get a lot. He asked how one thread can safely read the initial write of v to x. That is to say, if we have: Thread 1: globalRef = new C(5); Thread 2: C ref = globalRef; if (ref != null) {   int r1 = ref.x; } How can we change this code to guarantee that the read in Thread 2 will see the value 5 for x (assuming that ref is not null)? Let's back up for a minute, and talk about why, in the code as written, the read in Thread 2 will not see the value 5 for x . Under the Java memory model, for one thread to be guaranteed to see an update made to a variable by another thread, there must be a happens-before relationship between the update and th

Volatile Fields and Synchronization

A question from a viewer. In my talk, I had a class that looked like this: class Future {  private volatile boolean ready;  private Obj data;  ...  public synchronized void setOnce(Obj o) {   if (ready) throw ...;   data = o;   ready = true;  }  public Object get() {   if (!ready)    return null;   return data;  } } The setOnce method is executed by one thread, and get is executed by another. The point of this example is to examine what difference the volatile modifier on ready makes. In this case, if ready is not marked volatile , the compiler can reorder the writes to data and ready . This has the result that the thread that invokes get can see the value true when it reads ready , even if data has not been set yet. Messy. The questioner asked why setOnce is synchronized. This is somewhat orthogonal to the example, which is why I didn't mention it in the talk. However, I thought it was important enough to include it on the slide. If this method is not synchronize

JMM Talk at Google

This fellow is rather handsome, dashing and well-informed . If you would like a primer on the Java memory model, this is a good place to start. I was running out of time toward the end, though, so the last 15 minutes may be a little rushed. Perhaps I'll do a two-part followup at some point. One thing I forgot to say, at the very beginning: The Java memory model defines how threads interact through memory. That's why it's called the Java memory model. It has nothing to do with garbage collection or the object model (except where multithreading is important to those things). If the mood strikes, please feel free to ask questions about the memory model, or anything I said in the talk. I put a note in the talk to ask questions at: http://jeremymanson.blogspot.com/2007/03/jmm-questions-go-here.html .

HashMap Behavior in JDK6

It is important to read the documentation. Lots of people don't seem to do this, and they get bitten by it. The iterator for java.util.HashSet is documented as follows: Returns an iterator over the elements in this set. The elements are returned in no particular order. Of course, I've now seen code that ignores this, so I thought I would draw a few underlines for the kind of folks who miss it. I wish to state for the record that I did not write this code . It turns out that HashSets (and HashMaps) don't just use the user-defined hashCode as the hash value for objects they are passed. They rehash the number that method returns with their own hash function, to defend against poor quality hash functions. This makes a lot of sense, as many people write bad hash functions. Anyway, this means that when they change this function, objects will hash to different places. Then, when iterators visit these items, they will be visited in a different order. I wouldn't be posting

Loop Invariant Code Motion and the Java Memory Model

I gave a talk on the Java memory model this afternoon, and there was a question from the audience which I didn't answer to my own satisfaction, so I am answering it again, for my vast readership of none. At issue was the following bit of code: class Animator implements Runnable {  private volatile boolean stop = false;  public void stop() { stop = true; }  public void run() {   while (!stop) {    oneStep();    try { Thread.sleep(100); } …;   }  }  private void oneStep() { /*...*/ } } This is a pretty common idiom. One thread calls run(), and another thread (eventually) calls stop(), which sets the stop flag to true , and halts the loop. This is fairly intuitive. What is not intuitive about this code is that if the variable is not declared volatile , then regardless of whether another thread calls stop(), this code is not guaranteed to terminate . A compiler (in this case, likely Java's JIT compiler) can: Look at the run() method, Notice that stop is never updated, and the

C++ Is Getting More Like Java Every Day

Lawrence Crowl gave an interesting talk at Google last week . It would seem that the next revision of the C++ standard is getting: A new memory model, Garbage collection, Concepts (which are more-or-less type checking for templates. This roughly boils down to "generics") Unicode support, A regular expression library, A for-each loop! This is all starting to sound terribly familiar. I'm starting to like that language! Now all they have to do is get rid of multiple inheritance and their awful operator overloading semantics, and replace the STL, and I'll be sold. The new standard will be C++0x. They haven't decided whether x is 8 or 9 yet. Knowing the standards process, I imagine it will be 9. I hope they keep x to single digits... Anyway, in addition to all of this, it is getting move semantics for rvalues, lots of new syntax for initializers, and buckets of other things. Check out the talk for more.