Deep understanding of the Java memory Model (iii) sequential consistency

Source: Internet
Author: User

Data competition and sequential consistency guarantee

Data competition exists when the program is not synchronizing correctly. The Java memory model specification defines data competition as follows:

Write a variable in a thread,

Read the same variable on another thread,

and writing and reading are not sorted by synchronization.

When the code contains data competition, the execution of the program often produces counterintuitive results (as is the case in the previous chapter). If a multithreaded program is synchronized correctly, it will be a program with no data competition.

JMM guarantees the memory consistency of the correctly synchronized multithreaded routines as follows:

If the program is synchronized correctly, the execution of the program will have sequential consistency (sequentially consistent)-that is, the execution result of the program is the same as that of the program in the sequential-consistent memory model (as we'll see right away, this is a very strong guarantee for programmers). Synchronization here refers to the generalized synchronization, including the correct use of common synchronous primitives (Lock,volatile and final).

Sequential consistent memory model

The sequential-consistency memory model is a theoretical reference model idealized by computer scientists, which provides a strong memory-visibility guarantee for programmers. The sequential consistency memory model has two main features:

All operations in a thread must be performed in the order of the program.

(regardless of whether the program is synchronized) all threads can see only a single order of operation execution. In a sequential, consistent memory model, each operation must be executed atomically and immediately visible to all threads.

The sequential consistency memory model provides the programmer with the following view:

Conceptually, the sequential consistency model has a single global memory, which can be connected to any thread by a switch that swings around. At the same time, each thread must perform a memory read/write operation in the order of the program. We can see from the above figure that at any point in time only one thread can connect to memory. When multiple threads execute concurrently, the switch device in the diagram can serialize all memory read/write operations for all threads.

For better understanding, here are two diagrams to further illustrate the characteristics of the sequential consistency model.

Suppose two threads A and B are executed concurrently. Where a thread has three operations, the order in which they are in the program is: A1->A2->A3. b Threads also have three operations, in the order they are in the program: B1->B2->B3.

Suppose these two threads use the monitor to synchronize correctly: The three operations of a thread release the monitor after execution, followed by a B thread acquiring the same monitor. The execution effect of the program in the sequential consistency model is shown in the following illustration:

Now let's assume that these two threads are not synchronized, and here is the schematic diagram of the unsynchronized program in the Sequential conformance model:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.