Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 402
Yatendra Avatar asked May 03 '10 16:05

Yatendra


People also ask

What is memory consistency error in Java?

Memory consistency errors occur when different threads have inconsistent views of what should be the same data. The causes of memory consistency errors are complex and beyond the scope of this tutorial. Fortunately, the programmer does not need a detailed understanding of these causes.

What problem can occur when two threads share memory?

Thread Interference Error. When multiple threads share the same memory, there is a chance that two or more different threads performing different operations on the same data interleave with each other and create inconsistent data in the memory.

How will you avoid the discrepancy that can occur of multiple threads run simultaneously?

Thread interference errors can be avoided by synchronizing access to shared variables.

What is data inconsistency in multithreading?

it basically grabs a task if no similar task is processing atm, then it creates a new thread and give it the task..


2 Answers

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.

like image 108
Vincent Ramdhanie Avatar answered Sep 27 '22 20:09

Vincent Ramdhanie


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.

like image 45
Alexander Pogrebnyak Avatar answered Sep 27 '22 21:09

Alexander Pogrebnyak