Saturday 13 May 2017

24. Volatile keyword

1. What is Volatile keyword?
  1. The Java volatile keyword is used to mark a Java variable as "being stored in main memory".
  2. More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.
  3. Actually, since Java 5 the volatile keyword guarantees more than just that volatile variables are written to and read from main memory.
  4. In a multithreaded application where the threads operate on non-volatile variables, each thread may copy variables from main memory into a CPU cache while working on them, for performance reasons.
  5. If your computer contains more than one CPU, each thread may run on a different CPU. That means, that each thread may copy the variables into the CPU cache of different CPUs.
  6. With non-volatile variables there are no guarantees about when the Java Virtual Machine (JVM) reads data from main memory into CPU caches, or writes data from CPU caches to main memory. This can cause several problems
  7. Imagine too, that only Thread 1 increments the counter variable, but both Thread 1 and Thread 2 may read the counter variable from time to time.
public class SharedObject {

   public int counter = 0;

}

  1. If the counter variable is not declared volatile there is no guarantee about when the value of the counter variable is written from the CPU cache back to main memory. This means, that the countervariable value in the CPU cache may not be the same as in main memory.
  2. The problem with threads not seeing the latest value of a variable because it has not yet been written back to main memory by another thread, is called a "visibility" problem. The updates of one thread are not visible to other threads.
  3. By declaring the counter variable volatile all writes to the counter variable will be written back to main memory immediately. Also, all reads of the counter variable will be read directly from main memory. Here is how the volatile declaration of the counter variable looks:

public class SharedObject {

   Public volatile int counter = 0;

}

  1. Declaring a variable volatile thus guarantees the visibility for other threads of writes to that variable.
2. What is Happens before relationship?
  1. A happens-before relationship between two program statements is sort a guarantee which ensures that any memory writes by one statement are visible to another statement.
  2. Since Java 5 the volatile keyword guarantees more than just the reading from and writing to main memory of variables.
  3. Actually, the volatile keyword guarantees this:
    1. If Thread A writes to a volatile variable and Thread B subsequently reads the same volatile variable, then all variables visible to Thread A before writing the volatile variable, will also be visible to Thread B after it has read the volatile variable.
    2. The reading and writing instructions of volatile variables cannot be reordered by the JVM (the JVM may reorder instructions for performance reasons as long as the JVM detects no change in program behaviour from the reordering).
    3. Instructions before and after can be reordered, but the volatile read or write cannot be mixed with these instructions. Whatever instructions follow a read or write of a volatile variable are guaranteed to happen after the read or write.
3. Synchronization supports Happens before relationship?
  1. Yes, Synchronization also supports Happens before relationship.
  2. If thread entered into synchronized area it will read the data from main memory to cpu caches of present thread.
  3. If thread leaving the synchronized area it will write the data to main memory from cpu cache of present thread.


No comments:

Post a Comment

3. Java Program to create Binary Tree