Detail the memory visibility of Java multi-thread

Source: Internet
Author: User
Tags volatile

visibility : A thread's modification of shared variable values can be seen by other threads in a timely manner.

Shared variables : If a variable has a copy in the working memory of multiple threads, then this variable is the number of lines
Shared variables of the process.

Java memory model :

Threads are required to operate on shared variables:

The principle of shared variable visibility implementation:


Requirements for Visibility:


How visibility is implemented: (Final also available)


The process by which threads execute mutually exclusive code:


The concept of reordering :


The concept of as-if-serial:


Causes of shared variables not being visible between threads:


Visibility Analysis:



When a thread accesses one of the sysnchronized (this) synchronization blocks of object, other threads will block access to all other synchronized (this) synchronization blocks in object

Volatile for visibility:


Volatile cannot achieve atomic analysis:


Solutions to ensure the atomicity of the self-increment operation:

Usage of Reentrantlock:



For volatile applications:


Comparison of synchronized and volatile

Detail the memory visibility of Java multi-thread

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.