Java Memory models and threads

Source: Internet
Author: User

Reference: In-depth understanding of Java Virtual machines: JVM advanced Features and Best Practices (2nd edition)

The term "memory model" can be understood as the abstraction of read-write access to a particular memory or cache under a specific operating protocol.

Java memory model (Java JMM)

Main memory vs. working memory

Each thread also has its own working memory, the working memory of the thread holds the copy of the master memory of the variable used by the thread, and all operations of the thread on the variable (read, assign, and so on) must be made in working memory and not directly read and written to the variables in main memory. There is no direct access to variables in the other's working memory between different threads, and the transfer of variable values between threads needs to be done through main memory.

Main memory mainly corresponds to the part of the object instance data in the Java heap, while working memory corresponds to some areas of the virtual machine stack.

Inter-memory interaction operations

Lock

Unlock

Read

Load

Assign (Assignment): A variable that acts on the working memory, assigning a variable that is received from the execution engine to the working memory, and performs this operation whenever the virtual opportunity is assigned to a byte-code instruction that assigns a value to the variable.

Use

Store

Write

If you want to copy a variable from main memory to working memory, perform the read and load operations sequentially, and if you want to synchronize the variables from the working memory back to the main memory, execute the store and write operations sequentially. Note that the Java memory model only requires that the two operations above must be executed sequentially, without guarantee of continuous execution. That is, between read and load, store and write can be inserted into other instructions, such as the main memory of the variable A, b access, a possible order is read a, read B, load B, load a.

Special rules for volatile variable types

The first is to ensure that this variable is visible to all threads, where "visibility" means that when a thread modifies the value of the variable, the new value is immediately known to other threads.

Assuming that T represents a thread, and V and W represent two volatile variables respectively, the most recent value must be flushed from main memory before each use of the read, load, take, assign, store, and write operations. Used to ensure that the modified values of the variable v are visible to other threads. In working memory, each change to V must be immediately synchronized back to main memory to ensure that other threads can see their changes to the variable v.

The second is to prohibit order reordering optimizations.

Volatile-modified variables are not optimized by instruction reordering, ensuring that the code executes in the same order as the program.

Characteristics: atomicity, visibility and ordering

The characteristics of this model. The Java memory model is built around the 3 characteristics of how atomicity, visibility, and ordering are handled in a concurrency process.

Atomicity (atomicity): Atomic variable operations that are directly guaranteed by the Java memory model include read, load, assign, use, store, and write, and we can roughly assume that the access read and write of the basic data type is atomic.

Visibility (Visibility): visibility means that when a thread modifies the value of a shared variable, other threads can immediately know the change. The Java memory model realizes visibility by synchronizing the new value back to main memory after the variable is modified, and by refreshing the variable value from the main memory before the variable is read, which relies on the main memory as the delivery medium.

Ordering (ordering), the natural ordering of Java programs can be summed up as a sentence: if you observe in this thread, all operations are ordered, and if another thread is observed in one thread, all operations are unordered. The first half of the sentence refers to "line range expression as a serial semantics" (Within-thread as-if-serial Semantics), the latter sentence refers to the "order reordering" phenomenon and "working memory and main memory synchronization delay" phenomenon.

The principle of antecedent occurrence

The first occurrence is a partial-order relationship between two operations defined in the Java memory model, and if operation a precedes operation B, it is said that the effect of operation A can be observed by Operation B before Operation B, "Impact" includes modifying the values of shared variables in memory, sending messages, calling methods, and so on.

Java and threading

Threads are the basic unit of CPU scheduling.

Both Windows and Linux editions are implemented using a one-to-one threading model, and a Java thread maps to a lightweight process, because the threading model provided by Windows and Linux systems is single-to

Java Thread Scheduling

Collaborative thread scheduling (cooperative threads-scheduling) and preemptive thread scheduling (preemptive threads-scheduling)

The execution time of the cooperative thread scheduling thread is controlled by the thread itself, and after the thread has executed its work, the system is actively notified to switch to another thread. The downside is also obvious: the thread execution time is not controllable, and even if a thread is writing a problem and has never told the system to switch threads, the program will always be stuck there.

Preemptive scheduling of multi-threaded systems, then each thread will be allocated by the system execution time, the thread of the switch is not determined by the thread itself (in Java, Thread.yield () can give up execution time, but to get the execution time, the thread itself is no way to do).

Thread state

Newly created (new)

Run (runable)

Wait indefinitely (waiting)

Waiting period (Timed waiting)

Blocking (Blocked)

End (Terminated)

In this chapter, we first understand the structure and operation of the virtual machine Java memory model, then explain the atom, visibility, order in the Java memory model of the embodiment, and finally introduced the principle of the first occurrence of the rules and use. In addition, we also learned how threads are implemented in the Java language.

Java Memory models and threads

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.