Multi-thread memory visibility

Source: Internet
Author: User

The concept of visibility

A thread changes the value of a shared variable to be seen by other threads in a timely manner

Shared variable Value: If a variable is used in the working memory of multiple threads and there is a copy, then this variable is a shared variable of these threads

Two important provisions:
  1. All operations of a thread on a shared variable must be made in its own working memory and cannot be read and written directly from main memory
  2. Variables in the working memory of other threads cannot be accessed directly between different threads, and inter-thread variables are worth passing through main memory to complete
JMM (Java Memory Model)
  1. All of the variables are stored in main memory
  2. Each thread has its own independent working memory, which holds a copy of the variable used by the thread (a copy of the variable in main memory)
Principle of realization of shared variable visibility

Thread 1 Flushes the updated shared variable in the working memory 1 to main memory, and the main memory updates the value of the latest shared variable to work memory 2, thread 2 can see the modification of thread 1 in time

Synchronize achieving visibility
    1. JMM Two articles on synchronize

      The current value of the shared variable must be flushed to main memory before the line threads unlocked
      Line Cheng is the value of the shared variable in the working memory will be emptied so that when using shared memory, the most recent value needs to be re-read from the main memory (lock and unlock required the same lock)

    2. Re-order As-if-serial
Volatile for visibility
    1. volatile keyword:

      Can guarantee the visibility of volatile variables
      The atomicity of volatile variable compound operations cannot be guaranteed

    2. How volatile can achieve memory visibility

      By adding a memory barrier and disable reordering optimizations to achieve
      When a write operation is performed on a variable, a store barrier command is added after the write operation
      Volatile variable execution operation, a load barrier command is added before the read operation

    3. Volatile does not guarantee atomicity

      When a-thread's working variable has read the value of a variable, even if the B thread changes the value of the variable and stores it in memory, the A-thread load is not triggered because the a thread reads the load.

    4. Atomicity of guaranteed variable self-increment operation

      Using the Synchronize keyword
      Using Reentrantlock (java.until.concurrent.locks)
      Using Atomicinterger
      (vava.util.concurrent.atomic)

    5. Volatile application occasions
      At the same time meet:
    • A write operation on a variable does not depend on its current value.

      Not satisfied: number++,count=count*5, etc.
      Meet: Boolean variable, record temperature change variables, etc.

    • The variable is not included in the invariant with other variables

      Not satisfied: Invariant type Low<up

Final can also guarantee memory visibility synchronize and volatile comparisons

Volatile does not require locks and does not block threads
Volatile reads are equivalent to locking from memory visibility, and volatile writes are equivalent to unlocking
Synchronize can guarantee both visibility and atomicity

Coding Personal Blog Link

Reference-Video

Multi-thread memory visibility

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.