Java memory model

Source: Internet
Author: User

Translation from Wiki encyclopedia: Https://en.wikipedia.org/wiki/Java_memory_model

I did not find the page directly on the wiki to edit the Chinese, I am in this translation, my own learning.

The Java memory model describes how threads interact with memory in Java programming. Together with a single-threaded execution code description, this memory model provides the meaning of the Java programming language.

The original Java memory model was developed in the 1995 and is widely seen as disruptive, preventing many runtime optimizations and not providing sufficient assurance of code security.

Environment

The Java programming language and platform provide threading capabilities. Synchronization between threads is difficult for developers to understand. The difficulty is that Java applications can run on many processors and operating systems. In order to summarize the behavior of a program, the Java designers decided to clearly define the possible behavior of all programs.

On a modern platform, the code that is written is usually not executed. It is re-organized by compilers, processors, and memory systems to achieve the highest performance. A multiprocessor architecture, separate processors must have their own local caches that are not synchronized with the main memory. Requiring multithreading to keep perfect synchronization is often undesirable because it is usually very performance-intensive. This means that there may be different values for the same shared memory in different threads for a certain amount of time.

In a single-threaded environment, it is easy to explain the execution of code. The traditional method requires the system to realize the "as-if-serial" semantics of single thread Independent. When a separate thread executes, it executes the program as if it were in sequence, even if the behavior of the line occurs unordered.

If single-threaded execution instructions are unordered, other threads may realize that these instructions are out of order even if the semantics of the first thread are not affected. For example, consider a thread with two of the following instructions, execute synchronously, and the x, y variables are initialized to 0:

Thread1 Thread2
X=1; int r1 = y;
y=2; int r2 = x;

If there is no reflow, and the value of read y in thread 2 is 2. Then the x that is then read should be 1. Because the write to X is earlier than the write to Y. If the write is re-queued, read y can return 2, while reading X can return 0.

This Java memory model defines the behavior allowed by multithreading, while the above reading is possible. It generates execution time constraints for consistent and reliable applications in both thread and main memory. This constraint makes it possible to execute code in a multithreaded environment, even in the face of dynamic compilers, processor and cache optimizations.

Memory model

When executing a single thread, the rules are simple, and the Java programming specification requires the JVM to obey the semantics of within-thread as-if-serial . This runtime (usually referred to as a subsystem of dynamic compilation, processor, and memory) for introducing any useful execution optimizations as long as the results of thread isolation are guaranteed to be accurate, he will also have a procedural declaration order executed.

There is a warning for as-if-serial semantics: Do not prevent different threads from owning different data polygons. The memory model provides a clear guide on which values can be returned when the data is read. The basic principle reflects that a single operation can be disorderly, as long as the as-if-serial semantics of the thread are not violated, and the behavior of communication between threads such as get release a lock, ensure that these behaviors take precedence and are visible to other threads. For example, the behavior before releasing the lock should be able to be recovered after the lock is visible and orderly.

There is a possibility here that there is a partial order called Happens-before order in all behavior through the program execution. This happens-before sequence classifies the sequence of programs, and if an action occurs before another action, it occurs before other actions in the Happens-before order. In addition, the act of releasing and subsequently acquiring a lock constitutes a happens-before connection. Read is used to allow the return of the write value, if this write is the last write of this variable.

Effect

The Java memory model is the first time that a complex memory model is attempted in a popular language. The increasingly popular concurrency synchronization system requires a technology and tools that provide clear semantics. Since then, the need for memory models has been widely accepted, and languages like C + + are beginning to be available.

Later you can view:

Memory Model (computing): https://en.wikipedia.org/wiki/Memory_model_ (programming)

Java concurrency:https://en.wikipedia.org/wiki/java_concurrency

--------------

The translation is very blunt, turned over or do not understand what the Java memory model is a model, I think is a guarantee that the concurrent threads can be executed in order to execute the tool, but how to implement the principle is not very clear.

There is also an article to look at:

Http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html

JSR 133 (Java Memory Model) FAQ

  

  

  

Java memory 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.