Java Memory Knowledge Collation

Source: Internet
Author: User

Due to the main excerpt, the reference documents are listed first:

http://ifeve.com/jmm-faq/

1, I understand that the Java memory model is in multi-processor, multi-threaded scenarios to ensure that the read and write in memory will not be ambiguous.

The Java memory model describes which behaviors are legal in multithreaded code and how threads interact through memory. It describes the relationship between "variables in a program" and "the underlying details of getting or storing them from memory or registers." The Java memory model correctly implements these things by using a variety of hardware and compiler optimizations. ”

2, synchronization, synchronization is not only mutually exclusive.

"Synchronization ensures that a thread is visible to other threads with the same monitor in a predictable way before synchronizing the block or in a memory write operation in the synchronization block"

"The monitor has the ability to invalidate the local processor cache, so the variables are reloaded from main memory, so that other threads ' modifications to the shared variable are made visible to the current thread"

3,volatile, the buffer is flushed after the write volatile, and the value of the volatile attribute is written to main memory. Volatile and non-volatile variables around it are not enough to reorder

"When thread a writes a volatile field F, if thread B reads f, then anything visible to thread a becomes visible to thread B." ”

------------------

8.12

Today I learned a better single-case notation, and no more double-check.

Attached double-check code (excerpt: http://blog.csdn.net/ritterliu/article/details/49744397)

Instance must add the volatile keyword, used to remove some of the processor optimization, but this code is really long ah, or more concise bar

classLazysingleton {Private volatile StaticLazysingleton instance =NULL; PrivateLazysingleton () {} Public StaticLazysingleton getinstance () {//First Judgment        if(Instance = =NULL) {              //Lock code block            synchronized(Lazysingleton.class) {                  //Second Judgment                if(Instance = =NULL) {instance=NewLazysingleton ();//Create an instance of a singleton                }              }          }          returninstance; }  } 

Also want to lazy mode, and want to not sync. Iodh initialization on Demand Holder

1 /**2 * Created by Jerry on 17-8-9.3  */4  Public classTestmain {5      Public Static voidMain (String args[]) {6Testmain A =testmain.getinstance ();7Testmain B =testmain.getinstance ();8         if(A = =b) {9System.out.println (true);Ten         } One  A     } -  -     Private Static classSingletoninstance { the         Private Static FinalTestmain instance =NewTestmain (); -     } -  -      Public StaticTestmain getinstance () { +         returnsingletoninstance.instance; -     } +}

Perfect ╮(╯▽╰)╭

Java Memory Knowledge Collation

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.