Java 3 single-instance mode

Source: Internet
Author: User

1. A Hungry man type

1  Packagesingleinstance;2 /**a hungry man, class loading creates instances, wastes resources, threads are safe*/3  Public classSINGLEDOME01 {4     //private construction methods to prevent direct new objects5     PrivateSingleDome01 () {6     }7     //when the class loads, the object is created8     Private StaticSingleDome01 instance =NewSingleDome01 ();9     //provides getter for easy access to private objectsTen      One      Public StaticSingleDome01 getinstance () { A         returninstance; -     } -}

2. Lazy Type

1  Packagesingleinstance;2 /**lazy-type, when called to instantiate, save resources, high concurrency thread is not secure*/3  Public classSINGLEDOME02 {4     //private construction methods to prevent direct new objects5     PrivateSingleDome02 () {6     }7     Private StaticSINGLEDOME02 instance;8     //provides getter for external calls to private objects, returning SINGLEDOME02 objects9      Public StaticSingleDome02 getinstance () {Ten         if(instance==NULL){ OneInstance =NewSingleDome02 (); A         } -         returninstance; -     } the}

Java 3 single-instance 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.