Java Thread - Memory consistency errors

java, multithreading, shared-memory

Solution

You can read up about Read After Write (RAW), Write after Write(WAW) and Write After Read (WAR) hazards to learn more about this topic. These hazards refer to pipelined processses but it really is the same problem that occurs with multi threading. It basically means that two different threads are updating the same location in memory and if you depend on these updates in a certain order then you may be surprised to see that you cannot guarantee the order in which the updates occur.

For example, if you have two statements:

  x = y + z;
  r = x + z;

in a single thread then you have no problem because the value of r will always be consistent. In multiple threads however, it is possible or either statement to occur first and the value of r is harder to predict.

Problem

I was reading a Sun's tutorial on Concurrency. But I couldn't understand exactly what memory consistency errors are? I googled about that but didn't find any helpful tutorial or article about that. I know that this question is a subjective one, so you can provide me links to articles on the above topic. It would be great if you explain it with a simple example.

Original source