volatile- bantamweight achieve concurrency and memory semantics

Original connection :( http://www.studyshare.cn/blog-front/blog/details/1163/0  )

A, volatile definitions

volatile variable is a member of concurrent programming in java modified class, a key member of the attribute or object. Java concurrent programming is the most lightweight concurrent implementation, to ensure that the modified variable is visible to multiple threads of memory. In a thread write, under multiple threads to read the scene, prefers to use the volatile keyword.

Second, the use case

Here with a thread-safe singleton pattern to illustrate the specific usage scenarios and volatile principle, first of all, thread-safe singleton pattern is a lot of interviewers like to examine a question of the interviewer on the network are talking about a variety of blog hungry Chinese-style, single lazy write mode embodiment mode, also mentioned embodiment mode, a single double-check mode, the dual mode embodiment checklist code as follows:

14253062-9dbba1a138b977e0.png

I believe many people think this is thread-safe singleton pattern, however it really is thread-safe? Actually not, why? The reason is simple, if two threads call getInstance () method when a thread is executing a wherein a first re-inspection, another thread is performing a second check of the weight instance = new Singlton () when, at this time examples of code phrase seems to us that only one, but translated into instructions to run the cpu time will become three:

(1) assigned to a stack pointer instance, (2) to the heap memory allocated Singlton object (3) to the stack pointer to the heap memory. Then cpu doing this three-step operation when the execution order will be reordering (cpu executed after reordering according to its own rules), that the order of execution may be (1) (3) (2), then the problem comes, when executed (1) after, if the first thread is performing the first re-examination, then the time will find that instance is not empty, then return directly. However, the actual object is not yet complete allocated heap memory, then it will cause only a null pointer instance. When we call member variables Singlton object, an error occurs.

Problems arose, then the solution is very simple, is to use the keyword --volatile theme of the article, on our object member variable to add volatile.

private static volatile Singlton instance ;

Third, in-depth analysis

To understand the need to understand the principles of volatile thorough knowledge of cpu command reordering, local cache memory barrier as well as the thread and the main memory.

Instruction reordering: The above example also illustrates the cpu in the implementation of our code of a first compiled into assembly language, eventually compile

After translated into binary instructions accessed on cpu registers and execution. I.e., an instruction code becomes n,

N cpu time implementation of this directive will not be honest in order to perform, but will according to certain rules (probably briefly after complex,

After the first addition, subtraction, etc.) to perform the re-ordering. This is the instruction reordering. Types are reordered reordering compiler optimization, ILP

Reordering, memory reordering system.

Memory barrier: This is a solution for the next reordering bring concurrency problems, has a modern cpu memory barrier types are:

StoreLoad Barriers, in addition there are three were: LoadLoad Barriers, StoreStore Barriers

And LoadStore Barriers.

14253062-0472fb62380e77ea.png

Thread local cache and main memory:

14253062-faa8d31b1982b6a1.png

(1), each thread has its own local cache, as shown above, the thread A, thread B will have its own local cache, the cache line for

Data process itself needed to be used.

(2), under normal circumstances, when the thread A is changed the value of its own local cache of a variable and immediately to main memory is not refreshed, similarly, thread

B is the same.

(3), if the variable to add a volatile modified, then the thread A is changed when the value of a local cache, cpu forces present thread A

The value of a variable cache are flushed to main memory, so that a value that is the main memory of a local cache of the value of the thread A in the agreement. And at the same time so that the thread

B in the local cache of a failure, then when thread B to use a variable local cache, and main memory to re-read and self-refresh

Own local cache. This is volatile memory semantics.

Then go back to explain why the case was added volatile modification ensures truly thread-safe, direct it in one sentence: volatile repair

Variable decorative insert relevant memory barrier when the underlying execution prohibition cpu command reordering. Specifically, a write operation in volalite

Before inserting StoreStore barrier, a write back operation is inserted StoreLoad barrier behind volatile read operation are successively inserted barrier LoadLoad

And LoadStore barrier.

Summary: volatile keyword modified variables have visibility of memory, reading of a volatile variable, always able to see (any thread) to

The last variable is written. Concurrency is achieved using volatile lock prefix instructions to the cpu bus lock message, while allowing volatile

Current local cache content resides thread forcibly brush into the main memory, and let others use the local cache of the variable is not effective when the other thread needs

The modified using volatile variables when it will re-read to the main memory.

This article is a blogger original article, welcome message exchange, reproduced, please indicate the original source.

java development tools Download and install tutorial Guinness, the point here .

More technical articles in here .

Guess you like

Origin blog.csdn.net/weixin_34050005/article/details/90908550