Java Memory Model & multithread-programming
2012-03-09 15:26
260 查看
转自:http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
实例问题:http://stackoverflow.com/questions/5307003/java-multi-threading-atomic-reference-assignmentWhat is a memory model, anyway?
In multiprocessor systems, processors generally have one or more layers of memory cache, which improves performance both by speeding access to data (because the data is closer to the processor)and reducing traffic on the shared memory bus (because many memory operations can be satisfied by local caches.) Memory caches can improve performance tremendously, but they present a host of new challenges. What, for example, happens when two processors examine
the same memory location at the same time? Under what conditions will they see the same value?
At the processor level, a memory model defines necessary and sufficient conditions for knowing that writes to memory by other processors are visible to the current processor, and writes by the
current processor are visible to other processors. Some processors exhibit a strong memory model, where all processors see exactly the same value for any given memory location at all times. Other processors exhibit a weaker memory model, where special instructions,
called memory barriers, are required to flush or invalidate the local processor cache in order to see writes made by other processors or make writes by this processor visible to others. These memory barriers are usually performed when lock and unlock actions
are taken; they are invisible to programmers in a high level language.
It can sometimes be easier to write programs for strong memory models, because of the reduced need for memory barriers. However, even on some of the strongest memory models, memory barriers are
often necessary; quite frequently their placement is counterintuitive. Recent trends in processor design have encouraged weaker memory models, because the relaxations they make for cache consistency allow for greater scalability across multiple processors
and larger amounts of memory.
The issue of when a write becomes visible to another thread is compounded by the compiler's reordering of code. For example, the compiler might decide that it is more efficient to move a write
operation later in the program; as long as this code motion does not change the program's semantics, it is free to do so. If a compiler defers an operation, another thread will not see it until it is performed; this mirrors the effect of caching.
Moreover, writes to memory can be moved earlier in a program; in this case, other threads might see a write before it actually "occurs" in the program. All of this flexibility is by design --
by giving the compiler, runtime, or hardware the flexibility to execute operations in the optimal order, within the bounds of the memory model, we can achieve higher performance.
A simple example of this can be seen in the following code:
Class Reordering { int x = 0, y = 0; public void writer() { x = 1; y = 2; } public void reader() { int r1 = y; int r2 = x; } }
Let's say that this code is executed in two threads concurrently, and the read of y sees the value 2. Because this write came after the write to x, the programmer might assume that the read of
x must see the value 1. However, the writes may have been reordered. If this takes place, then the write to y could happen, the reads of both variables could follow, and then the write to x could take place. The result would be that r1 has the value 2, but
r2 has the value 0.
The Java Memory Model describes what behaviors are legal in multithreaded code, and how threads may interact through memory. It describes the relationship between variables in a program and the
low-level details of storing and retrieving them to and from memory or registers in a real computer system. It does this in a way that can be implemented correctly using a wide variety of hardware and a wide variety of compiler optimizations.
Java includes several language constructs, including volatile, final, and synchronized, which are intended to help the programmer describe a program's concurrency requirements to the compiler.
The Java Memory Model defines the behavior of volatile and synchronized, and, more importantly, ensures that a correctly synchronized Java program runs correctly on all processor architectures.
Do other languages, like C++, have a memory model?
Most other programming languages, such as C and C++, were not designed with direct support for multithreading. The protections that these languages offer against the kinds of reorderings thattake place in compilers and architectures are heavily dependent on the guarantees provided by the threading libraries used (such as pthreads), the compiler used, and the platform on which the code is run.
相关文章推荐
- Memory space manipulating in Java(Section two:Process Memory Model on AIX part three-The Large & Very Large Memory Model)
- Java Memory Architecture (Model), Garbage Collection and Memory Leaks
- TestDefaultTableModel--table model in java&sql
- 定时任务起的java进程没有释放导致oracle的问题not availavle & out of memory
- Exception in thread "TimerTotal_3" java.lang.OutOfMemoryError: Java heap space
- 【未解决】Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- <java><JTable>使用AbstractTableModel & JTable实现号码的分布图(初稿)
- Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: GC overhead limit exceede
- Exception in thread "http-apr-8080-exec-6" java.lang.OutOfMemoryError: PermGen space 解决!
- Eclipse 报 “Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”错误的解决办法
- Java (JVM) Memory Model and Garbage Collection Monitoring Tuning
- Algorithms(4th Edition),Java Programming Model
- myeclipse运行tomcat报错Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
- tomcate 报 PermGen space 错误Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
- JVM(十)Java 内存模型(Java Memory Model,JMM)
- java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "javamelody jenkins
- 浅析java内存模型--JMM(Java Memory Model)
- Java Memory Model work with Hardware Memory Architecture
- 解决办法:matlab Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap
- 解决Exception in thread "main" java.lang.OutOfMemoryError: Java heap space问题