Java Thread - Memory consistency errors
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.
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.
Basically in absence of any synchronization threads can see a different value of a simple field. Consider this example:
class Foo
{
int bar = 0;
void unsafeCall ( )
{
final Foo thisObj = this;
Runnable r = new Runnable ( )
{
public void run ( )
{
thisObj.bar = 1;
}
}
Thread t = new Thread(r);
t.start( );
Thread.sleep( 1000 );
// May print bar = 0
System.out.println( "bar = " + bar );
}
}
The simplest way of avoiding memory consistency error is to declare bar
field to be volatile
(see here for more details: https://www.ibm.com/developerworks/java/library/j-jtp06197/).
This forcing of threads to recheck the memory is called memory barrier. One other example of memory barrier is a synchronized
method/block.
Hm. They are basically talking about "visibility problems" and "re-ordering problems" (that terminology is more common at least in Java IMO). I think this link:http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#volatile explains what the tutorial is talking about, using more common terms (perhaps sun tried to use "easier" vocabulary or something).
I find a good example when searching this question. As below:
Accesses to main memory might not occur in the same order that the CPU initiated them, particularly for writes (which often go through hardware write buffers so the CPU needn't wait for them). If CPU 1 writes the Answer to location A and then writes the AnswerIsReady flag to B, CPU 2 may see the change to B before it sees the change to A, and thus get the WrongAnswer. Making either or both writes atomic doesn't help; what's needed is something called a "memory barrier."
via http://www.velocityreviews.com/forums/t390825-memory-consistency-errors.html
If you'd like to get a deeper understanding of shared memory consistency models, I'd refer you to the following tutorial.
http://rsim.cs.uiuc.edu/~sadve/Publications/computer96.pdf
精彩评论