Single-Case mode

Source: Internet
Author: User

*Lazy Type: (Multithreading high concurrent access to consider synchronization issues)>code, take the user entity as example (user) Publiucclassuser{/*1. User entity, static global dependency, class load is initialized to null pointer, no memory address, but also * is can reference*/                Private StaticUser instance=NULL; //2. Private constructors, external cannot have newly created objects in heap memory with the new keyword                PrivateUser () {}//Lazy Load instantiated objects                 Public Static synchronizedUser Getinstace () {//if the instantiated object is a null pointer                    if(!instancre) {                        //instantiate the current objectInstance=NewUser (); returninstance; }Else{                        //Otherwise, the currently instantiated object is returned                        returninstance; }                }            }    *a hungry man type:>code, take the user entity as an example: Public classuser{//class loading is the initialization of the user object, a hungry man, starvation loading,                Private StaticUser instance=NewUser (); //for external access interfaces, which are instantiated objects for external access                 Public StaticUser getinstance () {returninstance; }                                    //private constructors, simulating databases                PrivateUser () {}} Note: In singleton mode, only one instance of a class is promised, so the constructor is private. *lazy is not to initialize the class at the time of class loading instantiation object, can not guarantee the uniqueness of the object, so in multi-threaded case, despite the null value of the instance object, it will also lead to thread safety problem, because multithreading is to run the thread according to the time slice, destroy the class code of the order of vertical execution, because        This plus the Sync keyword synchronized, is code block synchronization, thread safety. *a hungry man because the static class object is instantiated when the class is loaded, there is only one static global dependent class object at any time, and there is no security problem with threads. 

Single-Case mode

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.