<span id="Label3"></p>JMM:<p><p>The Java memory model, which is built around the three characteristics of <span style="color: #ff0000;">visibility</span> , <span style="color: #ff0000;">atomicity</span> , and <span style="color: #ff0000;">ordering</span> in concurrent processes, is based on a model.</p></p>Visibility Of:<p><p>JMM provides a volatile variable definition, final, synchronized block to ensure Visibility.<br>For example, when thread A writes the shared variable x=1 to primary memory, how to ensure that thread B reads the value of the shared variable x is 1, which is what JMM does. JMM provides a memory visibility guarantee for Java programmers by controlling the interaction between main memory and the local memory of each THREAD.</p></p>Atomic nature:<p><p>JMM provides guaranteed access to the basic data type of atomicity (in fact, writing a working memory variable to main memory is a major two-step process: store, write), but the actual business processing scenario often requires a greater range of atomicity guarantees, so the model also provides <span style="color: #ff0000;">synchronized </span>block to ensure</p></p>Order:<p><p>This concept is relative, if in this thread, all operations are orderly, if one thread observes another thread, all operations are unordered, the preceding sentence is "line range behaves as a serial behavior", the latter sentence is "reordering of instructions" and "working memory and main memory synchronization delay" phenomenon, the model provides <span style="color: #ff0000;"> Volatile and synchronized to ensure the order of operations between Threads</span> .</p></p>Re-order:<p><p>In order to improve performance when executing a program, compilers and processors often reorder instructions (compilers, Processors) because of these reordering, which can cause memory visibility issues (data security issues) and ordering problems for multithreaded Programs.<br>How is the JMM handled?<br>COMPILER-JMM for compilers prohibit specific types of compilers from being reordered<br>For processor reordering, The JMM collation of the handler will require the Java compiler to insert a specific type of memory barrier when generating the sequence of instructions (memories Barriers,intel called the Fence) instruction, Suppress a particular type of handler reordering by using a memory barrier directive<br>In short, JMM provides a consistent memory visibility guarantee for programmers by prohibiting certain types of compilers from being reordered and handlers being Reordered.<br><br> <span style="color: #ff0000;">A thread specific when to refresh the shared data to main memory is uncertain, assuming we use the synchronization primitive (synchronized,volatile and final), then the refresh time is determined, for example: thread A release lock will be synchronized to the main memory, When thread B acquires the lock, it synchronizes the main memory data, that is, the "a thread frees the lock--b thread acquires the lock" can implement communication between a a/b Thread.</span></p></p>Java Memory Model Happens-before law<p>The rules for Happens-before is:<br>Program Order Rule. Each of the action in a thread happens-before every action in this thread that comes later in the program Order.<br>Monitor Lock Rule. An unlock in a monitor lock happens-before every subsequent lock on that same monitor lock.<br>Volatile variable Rule. A write to a volatile field happens-before every subsequent read the That same field.<br>Thread Start Rule. A call to Thread.Start on a thread happens-before every action in the started Thread.<br>Thread termination Rule. Any action with a thread happens-before any and thread detects that thread have terminated, either by successfully return F Rom Thread.Join or by thread.isalive returning FALSE.<br>Interruption Rule. A thread calling interrupt on another thread happens-before the interrupted thread detects the interrupt (either by have Interruptedexception thrown, or invoking isinterrupted or interrupted).<br>Finalizer Rule. The end of a constructor for a object Happens-before the start of the finalizer for that Object.<br>Transitivity. If a happens-before b, and b happens-before c, then a happens-before C.<br>----------------------------</p>What is happens-before?<p><p>Happens-before is "what what must be run before what," that is to guarantee the Order.<br>Because the CPU can not do in the order of our code to execute the memory of the access process, that is, instructions will be disorderly or parallel operation,<br>The order is guaranteed only if the above Happens-before is Specified.</p></p>Why are there visibility issues?<p><p> The following is a brief introduction. Relative to memory, the speed of the CPU is very high, if the CPU needs to access data directly with the memory, during the access process, the CPU will always be idle, this is a great waste, my mother said, waste is not good, so, The modern CPU has a lot of registers, multi-level cache, They have a much higher speed of access than memory. When a thread executes, a piece of data in memory exists in the working store of that thread (the <strong> working memory, An abstraction of the cache and register, is derived from the Concurrent programming in Java:design Principles and Patterns, Second Edition §2.2.7, original: Every thread is defined to has a working memory (an abstraction of Cach Es and Registers) in which to store Values. A lot of people think that working memory is a part of ram, which may be translated into working memory for the sake of work, in order to avoid confusion, <span style="color: #ff0000;"> This is called working storage, Each thread has its own work store, </span> </strong>), and writes back to memory at a particular Time. Single thread, This is no problem, if it is multithreaded to access the same variable at the same time? A variable in memory exists in more than one work store, and thread 1 modifies the value of variable a when it is visible to thread 2. In addition, the compiler or runtime, in order to be efficient, can <strong> reorder </strong> the commands when they are allowed, and the order in which they are reordered is inconsistent with the code so that thread 2 reads a variable and thread 1 may not have been written yet. Although the code sequence is on the previous write Operation. This is the origin of the visibility problem. </p></p><p><p> also, the cache between multiple CPUs is not guaranteed to be synchronized in real time, <br> that is, you just assigned a variable, another thread immediately gets its value, it may get the old value (or null), <br> Because two threads are executed on different cpus, They see a different cache value, <br> only in synchronized or volatile or final conditions can guarantee correctness, <br> Many people only remember the function of lock when they use synchronized, And forget about the problem of visibility between threads. </p></p><pre><pre> <span style="color: #0000ff;">public </span> <span style="color: #0000ff;">class </span> <span style="color: #000000;" "> Test {</span> <span style=" color: #0000ff; ">private </span> <span style=" color: # 0000ff; ">int </span> <span style="color: #000000;"> n; </span> <span style="color: #0000ff;">public </span> <span style="color: #0000ff;">void </span> set (<span style=" Color: #0000ff; ">int </span> <span style="color: #000000;"> N) {</span> <span style="color: #0000ff;">this </span>. n =<span style="col Or: #000000; "> n; </span> <span style="color: #0000ff;">public </span> <span style="color: #0000ff;">void </span> <span style="color: #000000; "> check () {</span> <span style="color: #0000ff;">if </span> (n!=<span style="color: #000000;"> N) </span><span style="color: #0000ff;">throw </span> <span style="color: #000000;" check error! " <span>); }}</span></pre></pre><p><p>The n! = N in check () seems to never be true because they point to the same value, but it is very likely to occur when it is not synchronized.<br><br>In addition, JMM does not guarantee the atomic nature of the creation process, when read and write concurrency, may see incomplete objects,<br>This is <span style="color: #ff0000;">why the famous "double check precedent" method in singleton mode is not feasible in Java</span> . (but the. NET memory model guarantees THIS)<br>of course, the <span style="color: #ff0000;">lazy loading of a singleton in Java can be implemented in another scenario (scenario Four)</span>:</p></p>Scenario One: Non-lazy Load Singleton class<pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #000000;">Singleton { </span> <span style="color: #0000ff;">private</span> <span style="color: #000000;"> Singleton () {} </span> <span style="color: #0000ff;">private</span><span style="color: #0000ff;">static</span><span style="color: #0000ff;">final</span><span style="color: #0000ff;">New </span> <span style="color: #000000;">Singleton (); </span> <span style="color: #0000ff;"></span>public<span style="color: #0000ff;">static</span> <span style="color: #000000;"> Singleton getinstance () { </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;"> instance; } }</span></pre></pre>Scenario Two: Simple Synchronous lazy loading<pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #000000;">Singleton { </span> <span style="color: #0000ff;">private</span><span style="color: #0000ff;">static</span><span style="color: #0000ff;">null</span><span style="color: #000000;">; </span> <span style="color: #0000ff;">public</span><span style="color: #0000ff;">static</span><span style="color: #0000ff;">synchronized</span> <span style="color: #000000;"> Singleton getinstance () { </span> <span style="color: #0000ff;">if</span> <span style="color: #0000ff;"> Null</span><span style="color: #000000;"></span><span style="color: #0000ff;">new</span> <span style="color: #000000;"> Singleton (); </span> <span style="color: #0000ff;">return</span><span style="color: #000000;"></span></pre></pre>Scenario Three: Double check precedent delay loading<p><p>The goal is to avoid too much synchronization,<br>But it doesn't work in java, because if (instance = = Null) Outside the synchronization block, you might see an instance that already exists but is not Complete.<br> <span style="color: #ff0000;">JDK5.0 later version if instance as volatile is feasible</span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Singleton {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>Singleton instance =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">Singleton getinstance () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(instance = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">synchronized</span></span>(Singleton.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(instance = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {instance =</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Singleton (); </span>}}}</span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">instance; } } </span></span></pre>Scenario Four: class Loader lazy loading<pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #000000;">Singleton { </span> <span style="color: #0000ff;">private</span><span style="color: #0000ff;">static</span><span style="color: #0000ff;">class</span> <span style="color: #000000;"> Holder { </span> <span style="color: #0000ff;">static</span><span style="color: #0000ff;">final< /c9><span style="color: #0000ff;">New</span> <span style="color: #000000;"> Singleton (); } </span> <span style="color: #0000ff;"></span>public<span style="color: #0000ff;">static</span> <span style="color: #000000;"> Singleton getinstance () { </span> <span style="color: #0000ff;">return</span></span><span style="color: #000000;"></span></pre></pre><p><p></p></p><p><p>JMM and Happens-before principles</p></p></span>
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