Java synchronization-three major properties of concurrency

Source: Internet
Author: User
Tags volatile

Java concurrency three major properties

In the Java memory model, there are three major properties: atomicity , ordering , and visibility .

atomicity : Either an operation or multiple operations, or all execution and execution of the process is not interrupted by any factor, or it is not executed. Even when multiple threads are executed together, an operation will not be disturbed by other threads once it is started.

Ordering: The order in which the program executes is executed in the order of the Code.

Visibility : When multiple threads access the same variable, once the thread modifies the value of the variable, other threads can RICO see the modified value.

Atomic Nature

For example, a wants to transfer 1000 dollars from his account to B's account. The process of transferring money from a to the end of a transfer is called a transaction. In this transaction, do the following:

    1. Subtract 1000 dollars from the account of a. If A's account originally had 3000 dollars, it would now be 2000 dollars.
    2. Add 1000 dollars to the account of B. If B's account had 2000 dollars, it would now be 3000 dollars.

If the account of a has been reduced by 1000 dollars, suddenly an accident, such as a power outage or something, causing the transfer transaction unexpectedly terminated, and at this time B's account has not increased by 1000 yuan. Well, we call this operation a failure, to roll back.

We call this either a successful or a failed operation called an atomic operation.

If a transaction can be thought of as a program, it is either completely executed or not executed at all. This characteristic is called atomicity .

To give an exact example:

int x = 1; int y = x;  

The above four lines of code, a glance is atomic operation, in fact, only the 1th sentence is atomic operation .

Let's start by understanding how the computer works:

When we run the program, the CPU executes each instruction of the program, in the process of executing the program, it will certainly involve some temporary data, the data is stored in memory. There is a problem with the CPU executing instructions much faster than reading from memory and writing data to memory. If the operation data is read and written from memory at any time, it will greatly reduce the running speed of the program, so there is a cache:

While the program is running, the data required for the operation is copied from memory to the CPU's cache, and when the CPU is calculated, it can read and write data directly from its cache. After the operation is finished, the data in the cache is then written to memory.

Let's go back and look at the four lines of code just now:

int x=1;//给x赋值为1,这个操作会直接将10写入内存中
int y=x;//这其实是两个操作,先读取x的值,然后将x的值赋值给y再写入内存中,这两个操作虽然都是原子性操作,但是合起来就不是原子性操作了
x++;x+=1;//这两句代码包含3个操作,先去读取x的值,然后进行加1,在把新值写入到内存中

But notice a question, the first sentence of code:

int x=1;//假设x是一个32位的变量,那为它赋值包含两个过程,给低16位赋值,给高16位赋值,所以也不是原子性操作
Visibility of

For visibility, Java provides the volatile keyword to guarantee visibility.

When a shared variable is modified by volatile, he will ensure that the change to the value is immediately visible to the other shared threads, so that the next time it is loaded, it will be re-evaluated from memory instead of the cache, but the volatile variable cannot guarantee atomicity.

If you want to guarantee atomicity, you can use synchronized and Reentrantlock, which can guarantee both visibility and atomicity, but they can be much more expensive.

Order of

In Java, the compiler and the processor are allowed to reorder the instructions, and what is called reordering, and the simple point is to change the order in which the instructions are executed. reordering does not affect single-threaded execution, but it can affect multi-threaded concurrency execution.

Similarly, synchronized and Reentrantlockcan be used, and they will also guarantee order .

But let's talk about something extra today, with some innate ordering in the Java memory model. Also known as the happens-before principle.

Happens-before principle:

    • Program Order rules: Within a thread, in code order, the preceding operation precedes the operation that is written in the back
    • Locking rule: A unlock operation occurs after the lock operation that faces the same lock
    • Volatile variable rule: The write operation of a variable precedes the read operation that faces the variable.
    • Delivery rule: If operation a precedes operation B, and Operation B precedes Operation C, it can be concluded that operation a precedes operation C
    • Thread Start rule: the Start () method of the thread object takes precedence over each action of this thread
    • Thread Break rule: The call to the thread interrupt () method occurs when the code of the interrupted thread detects that the interrupt event occurred
    • Thread termination rule: All operations in a thread occur first in thread termination detection, and we can detect that the thread has terminated execution by means of the Thread.Join () method end, Thread.isalive () return value
    • Object Finalization rule: Initialization of an object occurs at the beginning of his finalize () method

Transferred from: https://www.cnblogs.com/dolphin0520/p/3920373.html

Java synchronization-three major properties of concurrency

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.