Java Memory Mannequin

2025年8月31日 (日) 09:05時点における192.126.234.26 (トーク)による版 (ページの作成:「<br>The Java memory model describes how threads in the Java programming language interact by way of [http://nanjangcultures.egreef.kr/bbs/board.php?bo_table=02_04&wr_id=2…」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)


The Java memory model describes how threads in the Java programming language interact by way of Memory Wave. Together with the description of single-threaded execution of code, the memory model offers the semantics of the Java programming language. The Java programming language and platform present thread capabilities. Synchronization between threads is notoriously difficult for builders; this problem is compounded because Java purposes can run on a wide range of processors and working methods. To be able to draw conclusions a couple of program's behavior, Java's designers decided they needed to clearly outline doable behaviors of all Java programs. On fashionable platforms, code is steadily not executed within the order it was written. It is reordered by the compiler, the processor and the memory subsystem to realize most efficiency. On multiprocessor architectures, individual processors might have their own native caches that are out of sync with main memory. It is mostly undesirable to require threads to stay completely in sync with each other as a result of this can be too pricey from a efficiency viewpoint.



Because of this, different threads may observe completely different values of the same shared knowledge at any given time. In a single-threaded environment, it is simple to purpose about code execution. The typical method requires the system to implement as-if-serial semantics for individual threads in isolation. When a person thread executes, it should appear as if all the actions taken by that thread occur within the order they seem in the program, even when the actions themselves happen out of order. If one thread executes its directions out of order, then another thread may see the fact that those directions had been executed out of order, even when that did not affect the semantics of the first thread. If no reorderings are carried out, and the read of y in Thread 2 returns the worth 2, then the subsequent read of x should return the worth 1, because the write to x was performed before the write to y. The Java Memory Mannequin (JMM) defines the allowable habits of multithreaded packages, and therefore describes when such reorderings are doable.



It places execution-time constraints on the relationship between threads and main memory so as to achieve consistent and dependable Java functions. By doing this, it makes it potential to cause about code execution in a multithreaded atmosphere, even in the face of optimizations carried out by the dynamic compiler, the processor(s), and the caches. For the execution of a single thread, the foundations are easy. The Java Language Specification requires a Java digital machine to observe within-thread as-if-serial semantics. The runtime (which, on this case, normally refers back to the dynamic compiler, the processor and the memory subsystem) is free to introduce any useful execution optimizations as long as the results of the thread in isolation is guaranteed to be exactly the identical as it would have been had all the statements been executed within the order the statements occurred in this system (additionally referred to as program order). The main caveat of this is that as-if-serial semantics don't prevent totally different threads from having different views of the information.
etfriends.com


The memory model provides clear guidance about what values are allowed to be returned when the data is learn. The basic rules indicate that particular person actions could be reordered, as lengthy as the as-if-serial semantics of the thread aren't violated, and actions that imply communication between threads, such as the acquisition or release of a lock, be certain that actions that happen previous to them are seen by different threads that see their results. For instance, all the things that happens earlier than the release of a lock will probably be seen to be ordered before and visible to the whole lot that happens after a subsequent acquisition of that very same lock. Mathematically, there is a partial order known as the occurs-earlier than order over all actions carried out by the program. The occurs-before order subsumes this system order; if one motion occurs before one other in this system order, it's going to occur before the opposite within the happens-before order.



As well as, MemoryWave Community releases and subsequent acquisitions of locks type edges within the happens-earlier than graph. A read is allowed to return the value of a write if that write is the last write to that variable earlier than the read along some path within the occurs-before order, or if the write is just not ordered with respect to that read within the happens-before order. The Java memory model was the primary attempt to supply a complete memory mannequin for a preferred programming language. It was justified by the growing prevalence of concurrent and parallel methods, and the necessity to offer tools and technologies with clear semantics for such techniques. Pugh, William (2000). "The Java memory mannequin is fatally flawed" (PDF). Concurrency: Apply and Expertise. Goetz, Brian (2004-02-24). "Fixing the Java Memory Mannequin, Half 2" (PDF). Jeremy Manson and Brian Goetz (February 2004). "JSR 133 (Java Memory Mannequin) FAQ". Retrieved 2010-10-18. The Java Memory Mannequin describes what behaviors are authorized in multithreaded code, Memory Wave and how threads could work together 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 may be applied accurately using a wide variety of hardware and a large variety of compiler optimizations. Java Language Specification, Oracle. Manson, Jeremy. "JSR-133 FAQ". Goetz, Brian (2004-02-24). "Fixing the Java Memory Mannequin, Half 1" (PDF). Java theory and observe: Fixing the Java Memory Model, half 1 - An article describing problems with the unique Java memory model. Java concept and apply: Fixing the Java Memory Mannequin, half 2 - Explains the modifications JSR 133 made to the Java memory mannequin.