Java design Pattern--Singleton mode

Source: Internet
Author: User

I. What is a single-case mode

The memory that the JVM manages is the only instance of a class that exists in the heap, by privatizing the constructor, A static method is provided to provide a unique instance. Singleton patterns are often divided into a hungry man singleton patterns and lazy singleton patterns. The A hungry man singleton mode creates its own instance when the class is initialized. The lazy pattern is to create an instance when an instance is needed.

Two. Advantages and disadvantages of the singleton model

Advantage: There is only one instance in memory so there is less memory overhead, and a single-case pattern can reduce system performance overhead for some scenarios where objects are created and destroyed with cost comparisons.

Cons: Concurrency problems under multithreading.

Three. Handling of single-instance mode in JVM garbage collection

On the Internet, it was mentioned that the singleton model would not be garbage collected. What I can prove is that it won't be recycled in the hotspot virtual machine. The Accessibility analysis algorithm adopted by the Hotspot virtual machine. The Referenceon of a singleton class is always pointing to the previous instance. The class object in the method area is not garbage Recycling. So the singleton is always accessible, so the singleton mode is not recycled in the hotspot virtual machine.

Four. Code implementation of the singleton mode


1. A hungry man single-case mode:

1 /**2 3      * @authorXg.qiu4 5 * @TODO single case mode of JAVA23 design mode6 7 * @date May 14, 2015 17:15:578 9      * @seeJDK 1.5Ten  One      */ A  -  Public classsingleton{ -  the /* -  - * 1. Private non-parametric constructors -  +  */ -  + PrivateSingleton () {} A  at /* -  - * 2. Private static instances of this class constant object -  -  */ -  in Private Final StaticSingletoninstance =NewSingleton (); -  to /* +  - * 3. Provide a way to open up (return constant object directly: instance) the  *  */ $ Panax Notoginseng  Public Staticsingletongetinstance () { -  the //Direct return +              returninstance; A  the } +  -}


2. Lazy Singleton Mode (unsafe under multi-thread):

1   /**2 3  * @authorXg.qiu4 5 * @TODO pattern of Java23 design: Lazy type6 7 * @date May 14, 2015 17:15:578 9  * @seeJDK 1.5Ten  One  */ A  -  Public classSingleton { -  the /* -  - * 1. Private non-parametric constructors -  +  */ -  + PrivateSingleton () {} A  at /* -  - * 2. Private static objects of this class do not need to be initialized -  -  */ -  in Private  StaticSingleton instance =NULL;  -  to /* +  - * 3. Provide a way to open up (return constant object directly: instance) the  *  */ $ Panax Notoginseng  Public StaticSingleton getinstance () { -  the if(NULL==instance) { +  A //plus the thread lock . the  + synchronized(Singleton.class){ -  $ if(NULL= = Instance) Instance =NewSingleton (); $  - } -  the } - Wuyi returninstance; the  - } Wu  -}

Java design Pattern--Singleton 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.