Java Memory Mannequin
페이지 정보
작성자 Randolph Burrow 작성일25-09-15 18:58 조회9회 댓글0건본문
The Java memory mannequin describes how threads within the Java programming language interact through memory. Together with the outline of single-threaded execution of code, the memory mannequin offers the semantics of the Java programming language. The Java programming language and platform present thread capabilities. Synchronization between threads is notoriously difficult for developers; this issue is compounded as a result of Java purposes can run on a wide range of processors and working programs. To be ready to draw conclusions a couple of program's habits, Java's designers decided they needed to clearly outline possible behaviors of all Java applications. On trendy platforms, code is often not executed in the order it was written. It's reordered by the compiler, the processor and the memory subsystem to attain maximum efficiency. On multiprocessor architectures, individual processors could have their very own local caches which are out of sync with primary memory. It is mostly undesirable to require threads to remain perfectly in sync with one another as a result of this could be too expensive from a performance point of view.
Because of this, different threads could observe totally different values of the same shared information at any given time. In a single-threaded environment, it is easy to cause about code execution. The everyday method requires the system to implement as-if-serial semantics for individual threads in isolation. When an individual thread executes, it would appear as if the entire actions taken by that thread occur in the order they appear in this system, even when the actions themselves happen out of order. If one thread executes its instructions out of order, then one other thread may see the truth that those directions were executed out of order, even when that didn't affect the semantics of the first thread. If no reorderings are carried out, and the learn of y in Thread 2 returns the worth 2, then the next learn of x ought to return the worth 1, because the write to x was performed before the write to y. The Java Memory Wave Audio Mannequin (JMM) defines the allowable behavior of multithreaded packages, and subsequently describes when such reorderings are possible.
It locations execution-time constraints on the relationship between threads and predominant memory so as to realize consistent and dependable Java functions. By doing this, it makes it doable to purpose about code execution in a multithreaded surroundings, 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 rules are simple. The Java Language Specification requires a Java digital machine to observe within-thread as-if-serial semantics. The runtime (which, on this case, often refers to the dynamic compiler, the processor and the memory subsystem) is free to introduce any useful execution optimizations so long as the results of the thread in isolation is assured to be exactly the identical as it will have been had all the statements been executed in the order the statements occurred in the program (additionally referred to as program order). The key caveat of this is that as-if-serial semantics do not stop different threads from having different views of the info.
The memory model gives clear steering about what values are allowed to be returned when the information is learn. The basic rules suggest that particular person actions can be reordered, as lengthy as the as-if-serial semantics of the thread aren't violated, and actions that indicate communication between threads, such as the acquisition or release of a lock, be certain that actions that happen prior to them are seen by different threads that see their effects. For example, every part that happens before the release of a lock will probably be seen to be ordered earlier than and visible to all the things that happens after a subsequent acquisition of that same lock. Mathematically, there is a partial order called the occurs-earlier than order over all actions performed by the program. The occurs-earlier than order subsumes the program order; if one action occurs before one other in the program order, it would happen before the other in the occurs-before order.
In addition, releases and subsequent acquisitions of locks form edges within the occurs-earlier than 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 alongside some path in the occurs-before order, or if the write will not be ordered with respect to that learn within the occurs-before order. The Java memory mannequin was the first attempt to offer a comprehensive memory model for a preferred programming language. It was justified by the growing prevalence of concurrent and parallel programs, and the necessity to supply instruments and technologies with clear semantics for such programs. Pugh, William (2000). "The Java memory mannequin is fatally flawed" (PDF). Concurrency: Follow 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 Model) FAQ". Retrieved 2010-10-18. The Java Memory Mannequin describes what behaviors are authorized in multithreaded code, and Memory Wave the way threads may interact by means of memory. It describes the connection between variables in a program and the low-stage details of storing and retrieving them to and from memory or registers in a real computer system. It does this in a means that may be implemented accurately using a large variety of hardware and a wide number of compiler optimizations. Java Language Specification, Oracle. Manson, Jeremy. "JSR-133 FAQ". Goetz, Brian (2004-02-24). "Fixing the Java Memory Mannequin, Part 1" (PDF). Java principle and follow: Memory Wave Fixing the Java Memory Model, half 1 - An article describing problems with the unique Java memory model. Java principle and apply: Fixing the Java Memory Mannequin, half 2 - Explains the changes JSR 133 made to the Java memory model.
댓글목록
등록된 댓글이 없습니다.