Java memory model.

 

Java Memory Model:

Ready knowledge: cache, cache coherency, hardware buffer: write buffer invalidation queue, reorder memory, memory barrier.

  1, Java memory model concept.

    First, in the case of concurrent computer system must solve two problems: first, a processor updates to shared variables can be read by other processors when or under what circumstances, you can see the problem. The second case, a processor has to update multiple shared variables, and other processors based on the order in which to read these updates, namely ordering problem. The model is used to solve these two problems, it is called the memory model. The different processors or operating system and has a different memory model, Java as a cross-platform language, in order to shield this difference, and for atomicity in concurrency, visibility, order issues, defines its own memory model, this model is called Java memory model.

    Java memory model specifies all the variables are stored in main memory, each thread also has its own working memory, working memory thread's saved a copy of main memory to be used by the thread variable, variable thread All operations are carried out in the working memory.

volatile、synchronized、final。

  1, volatile: volatile when a variable is modified, it will have two characteristics. First: visibility, that a thread has been modified to the variables, other threads will know immediately. How it works: When volatile variables write operation, data will be modified immediately written back to memory, working memory and other data is invalid thread, other threads will get data from main memory in use, so the resulting value is the latest. Second: disable command reordering. How it works: When volatile variables operation will be more to execute an instruction at the beginning of the lock, it acts as a memory barrier, to disable command reordering.

Guess you like

Origin www.cnblogs.com/zhangyuhao/p/11767066.html