volatile resolve

volatile synchronization mechanism is provided by a lightweight java, it can be understood as a synchronization lock variable. Compared to heavyweight lock synchronized concerned, most of the time lock synchronized method or block of code, and lock a variable volatile but, more lightweight so volatile, of course, now increasingly synchronized jdk performance optimization, there is no imagined so heavy, in peacetime need to ensure a thread-safe method, you can rest assured that the use synchronized.

principle

The computer system performing the calculation, cpu reads data from the cache memory in its own register (cpu very small space in a memory space, easy to calculate the value, do not always read the memory), the computer now most are multicore computers, a plurality of cpu, cpu registers where the other is not visible, as in FIG.

1577063433170

When the type of a variable is declared as volatile, this variable will not be cached in memory cpu registers, every time direct operation, so every time other threads are reading the latest value.

characteristic

  • Variable Visibility: volatile variable is visible to all threads, one thread modifies the value of a variable, then the other thread will get the new value immediately.
  • Prohibit instruction reordering: the compiler and jvm cpu in, sometimes reorders order to optimize the efficiency of the operation would be a normal instruction, volatile variable would prohibit instruction reordering.

For example

We are familiar with the singleton pattern, there is a security wording, double check the following wording

public class Single {

    private static volatile Single single = null;

    private Single() {
    }

    public static Single getInstance() {
        if (single == null) {
            synchronized (single) {
                if (single == null) {
                    single = new Single();
                }
            }
        }
        return single;
    }
}复制代码

Single objects declared in the Code became a volatile subject, if there is no case volatile will happen? We look at the single = new Single () of this code, normal operation would be like this:

  1. Allocate memory M;
  2. Single Object initialized in the memory M;
  3. The single M address assigned to the object;

But there will compile command rearrangement operation would this be:

  1. Allocate memory M;
  2. The single M address assigned to the object;
  3. Single Object initialized in the memory M;

Back to the code, when the second step () A thread of execution in single = new Single, B thread into getInstance () method, first determine if (single == null), then have to have a memory address of the object on the single , B thread will return directly to single objects, but this thread get an empty object has not been initialized object memory M.

When the single variable declared as volatile, such instructions will avoid the problem of rearrangement.

in conclusion

When we program in multiple threads may occur when operating with a shared variable, thread-safe to ensure the shared variable, the variable can be declared as volatile.

Guess you like

Origin juejin.im/post/5e1c1bd8518825261546e316