Java Memory Model

Java Memory Model

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

The original Java memory model, developed in 1995, was widely perceived as broken, preventing many runtime optimizations and not providing strong enough guarantees for code safety. It was updated through the Java Community Process, as Java Specification Request 133 (JSR-133), which took effect in 2004, for Tiger (Java 5.0).

Context

The Java programming language and platform provide thread capabilities. Thread synchronization is notoriously difficult for developers, and the problem is magnified because a Java application could run on a wide range of processors or operating systems. In order to be able to draw conclusions about the behavior of a program, Java's designers decided that it was necessary to define clearly and completely the possible behaviors of all Java programs.

On modern platforms, code is frequently not executed in the order it was written. It is reordered by the compiler, the processor and the memory subsystem to achieve maximum performance. On multiprocessor architectures, individual processors may have their own local caches that are out of sync with main memory. It is generally undesirable to require threads to remain perfectly in sync with one another because this would be too costly from a performance point of view. This means that at any given time, different threads may see different values for the same shared data.

In a single-threaded environment, it is easy to reason about code execution. The typical approach requires the system to implement "as-if-serial" semantics for individual threads in isolation. When an individual thread executes, it will appear as if all of the actions taken by that thread occur in the order they appear in the program, even if the actions themselves occur out of order.

If one thread executes its instructions out of order, then another thread might see the fact that those instructions were executed out of order, even if that did not affect the semantics of the first thread. For example, consider two threads with the following instructions, executing concurrently:

If no reorderings are performed, and the read of y in Thread 2 returns the value 1, then the subsequent read of x should also return the value 1, because the write to x was performed before the write to y. However, if the two writes are reordered, then the read of y can return the value 1 and the read of x can return the value 0.

The Java Memory Model (JMM) defines the allowable behavior of multithreaded programs, and therefore describes when such reorderings are possible. It places execution-time constraints on the relationship between threads and main memory in order to achieve consistent and reliable Java applications. By doing this, it makes it possible to reason about code execution in a multithreaded environment, even in the face of optimizations performed by the dynamic compiler, the processor(s) and the caches.

The Memory Model

For execution of a single thread, the rules are simple. The Java Language Specification requires a Java Virtual Machine to observe "within-thread as-if-serial" semantics. The runtime (which, in this case, usually refers to the dynamic compiler, the processor and the memory subsystem) is free to introduce any useful execution optimizations so long as the result of the thread in isolation is guaranteed to be exactly the same it would have been had all the statements been executed in the order the statements occurred in the program (also called program order).

The major caveat of this is that as-if-serial semantics do not prevent different threads from having different views of the data. The memory model provides clear guidance about what values are allowed to be returned when the data are read. The basic rules imply that individual actions can be reordered, as long as as-if-serial semantics are not violated, and actions that imply communication between threads, such as the acquisition or release of a lock, ensure that actions that happen prior to them are seen by other threads that see their effects. For example, everything that happens before the release of a lock will be seen to be ordered before and visible to everything that happens after a subsequent acquisition of that same lock.

Mathematically, there is considered to be a partial order called the happens-before order over all actions performed by the program. The happens-before order subsumes the program order; if one action occurs before another in the program order, it will occur before the other in the happens-before order. In addition, releases and subsequent acquires of locks form edges in the happens-before graph. A read is allowed to return the value of a write if that write is the last write to that variable before the read along some path in the happens-before order, or if the write is not ordered with respect to that read in the happens-before order.

Impact

The Java memory model was the first attempt to provide a comprehensive memory model for a popular programming language. It was justified by the increasing prevalence of concurrent and parallel systems, and the need to provide tools and technologies with clear semantics for such systems. Since then, the need for a memory model has been more widely accepted, and efforts are underway to provide such semantics for languages like C++ [http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/] .

ee also

* Memory model (computing)

External links

* [http://www-128.ibm.com/developerworks/library/j-jtp02244.html Java theory and practice: Fixing the Java Memory Model, part 1] - An article describing problems with the original Java memory model.
* [http://www-128.ibm.com/developerworks/library/j-jtp03304/ Java theory and practice: Fixing the Java Memory Model, part 2] - Explains the changes JSR 133 made to the Java memory model.
* [http://www.cs.umd.edu/users/jmanson/java/popl05.pdf study explaining the new Java 5.0 memory model]
* [http://jcp.org/en/jsr/detail?id=133 JSR-133 webpage]
* [http://www.cs.umd.edu/users/pugh/java/memoryModel/jsr-133-faq.html JSR-133 FAQ]
* [http://g.oswego.edu/dl/jmm/cookbook.html JSR-133 implementation guide]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Memory model — may refer to: Psychology Atkinson–Shiffrin memory model Baddeley s model of working memory Memory prediction model Informatics In computer software, a memory model describes how threads interact through memory, or what assumptions the compiler… …   Wikipedia

  • Memory model (computing) — In computing, a memory model describes the interactions of threads through memory and specifies the assumptions the compiler is allowed to make when generating code for segmented memory or paged memory platforms. History and significance A memory …   Wikipedia

  • Memory barrier — Memory barrier, also known as membar or memory fence or fence instruction, is a type of barrier and a class of instruction which causes a central processing unit (CPU) or compiler to enforce an ordering constraint on memory operations issued… …   Wikipedia

  • Java Specification Request — Java Specification Requests Java Specification Requests (JSR) est un système normalisé ayant pour but de faire évoluer la plateforme Java. Sommaire 1 Présentation 2 Implémentation 3 Interopérabilité informatique …   Wikipédia en Français

  • Java Specification Requests — (JSR) est un système normalisé ayant pour but de faire évoluer la plateforme Java. Sommaire 1 Présentation 2 Implémentation 3 Liste des JSRs 4 Notes et …   Wikipédia en Français

  • Java concurrency — The Java language and the JVM (Java Virtual Machine) have been designed to support concurrent programming, and all execution in takes place in the context of threads. Objects and resources can be accessed by many separate threads; each thread has …   Wikipedia

  • Java version history — The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community Process (JCP) …   Wikipedia

  • Java backporting tools — are programs (usually written in Java) that convert Java classes bytecodes from one version of the Java Platform to an older one (for example Java 5.0 backported to 1.4). Contents 1 Java backporting tools comparison 1.1 Main information 1.2… …   Wikipedia

  • Java (software platform) — Not to be confused with JavaScript. Java Original author(s) Oracle Corporation Developer(s) James Gosling / Sun Microsystems …   Wikipedia

  • Java Platform, Standard Edition — or Java SE is a widely used platform for programming in the Java language. It is the Java Platform used to deploy portable applications for general use.In practical terms, Java SE consists of a virtual machine, which must be used to run Java… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”