(2) synchronized and volatile underlying artistic realization of the principle of concurrent programming Java

The definition and implementation of volatile

Java programming language allows threads to access shared variables, variables can be shared in order to ensure accurate and consistent updates, exclusive lock thread should get through this variable alone. If a field is declared volatile, Java thread memory model ensures that all threads see the value of this variable is the same.

Lock prefix

Volatile variables when modified to read and write shared variables, JVM sends a Lock prefix instructions to the processor, the instruction prefix Lock multicore CPU processor raises the following two things
(1) the current processor cache line data back to system memory
(2) this operation will be written back to memory in other CPU caches data in the memory address is not valid.
In order to increase the processing speed, memory and processor does not communicate directly, but first the system memory to read data internal cache (L1, L2 or other) before the operation, but the operation is not finished know when memory writes. If you declare the volatile variables write, JVM will be sent to a processor Lock prefix instructions , where this variable data cache line is written back to system memory. If another processor cache values or old, and then perform calculations or operations problems. Therefore, in multi-threaded programming, in order to ensure value for each processor cache of the same, it will cache coherency , each processor to check whether the value of its own cache of date by sniffing data traveling in the bus, when the processor finds himself cache line corresponding to the memory address is modified, the current cache line will be set to inactive the processor, when the processor of the data modification operations, the system will re from read data memory processor cache.

volatile realization of the principle of two

(1) Lock prefix instructions cause the processor to write back cache memory
(2) cache of a processor is written back to causes other processor's cache memory invalid

Principles and Applications of synchronized implementation

Each object in Java can be used as a lock.
(1) For normal synchronization method, the lock is the current instance of the object
(2) for static synchronization method, the lock is Class object of the current class
(3) for block synchronization method, the object is synchronized lock brackets configuration.
When a thread attempts to access synchronized block, he must first obtain a lock, the lock must be released or launched when an exception is thrown.

Java object header

The lock is synchronized with the advance of the presence of Java objects.
The composition object header Mark Word and Class Metadata Address Array length or

Mark Word

The default storage object HashCode, generational age and the flag.
32 the JVM Mark Word default storage configuration table

Lock status 25bit 4bit 1bit whether it is biased locking 2bit lock flag
Lock-free status hashCode object Generational age of the subject 0 01

During operation, the data stored in the Mark Word will vary with the lock flag.
Mark Word table state change
Here Insert Picture Description
in the virtual machine 64, a 64bit Mark Word size, which storage structure table
Here Insert Picture Description

synchronized in the realization of the principle of the JVM

JVM based on entry and exit Monitor objects to implement the method of code synchronization and block synchronization, but both implementations are not the same. Synchronization code block using monitor enter and monitorexit instructions, with the synchronization method is used in another manner.
monitorenter instruction is compiled at the start position of the synchronization code is inserted into the block and monitorexit is inserted into the end of the methods and exception, the JVM monitorenter to ensure that each must have a corresponding paired monitorexit. Any object has a monitor associated with it, and when a monitor after being held, it is locked. When the thread execution to monitorenter instruction, will attempt to acquire ownership of the object corresponding to the monitor, that is trying to get a lock object.

Spinlocks

It refers to the time when a thread to acquire the lock, if the lock has been acquired by other threads, then the thread will wait for the cycle, and then continue to determine whether the lock is successfully acquired, until the lock is acquired will exit the loop.

CAS algorithm

I.e., compare and swap (compare and swap) is a well-known lock-free algorithms. Lock-free programming, that is, when not in use lock variable synchronization between multiple threads, which is synchronized variable in the absence of the thread is blocked, it is also called non-blocking synchronization (Non-blocking Synchronization). CAS algorithm involves three operands

The need to read and write memory value V

Comparing value A

B intends to write the new value

If and only if the value is equal to V A, CAS B with a new value by updating the value of V atomic manner, it will not perform any operations (compare and substitutions are an atomic operation). Normally a spin operation, i.e. continuously retries.

Published 24 original articles · won praise 1 · views 549

Guess you like

Origin blog.csdn.net/qq_45366515/article/details/105116001