"Reading" Two, single case mode

Source: Internet
Author: User

Characteristics:

1. Constructors are not put, they are generally private

2. Return a singleton object by exposing a static method or enumeration of public

3. Under a multithreaded environment , ensure that the Singleton class object has only one

4. Ensure that the object is not rebuilt when the Singleton class object is deserialized

Lazy mode:

1  Public classsingleton{2     Private StaticSingleton instance;3     //1. The constructor is not open to the outside4     Privatesingleton{5     }6     //2. Returning a singleton class object through a static method7     //3. Add synchronized, in the multi-threaded access, only one thread can be used in the same time synchronized modified methods or blocks of code. 8     //That is, getinstance is a synchronous method that guarantees that the object is unique under multiple threads9      Public Static synchronizedSingleton getinstance () {Ten         if(Instance = =NULL){ OneInstance =NewSingleton (); A         } -         returninstance; -     } the      -}

Advantage: Need time to instantiate, save resources

Cons: Each call to getinstance () is synchronized, causing unnecessary synchronization overhead

Double Checklock to implement a single example:

1  Public classsingleton{2     Private volatile StaticSingleton Singleton;3     PrivateSingleton () {}4      Public StaticSingleton getinstance () {5         //The first layer is empty to avoid unnecessary synchronization.6         //so only the first time will be locked, and then will not lock7         if(Singleton = =NULL) {8             synchronized(Singleton.class) {9                 if(Singleton = =NULL) {Ten                     //The second layer is empty to create an instance in the case of NULL OneSingleton =NewSingleton (); A                 } -             } -         } the         returnSingleton; -     } -}

Note that the DCL is problematic:

New Singleton ();

This is not an atomic operation, it can divide three things:

(1). Allocating memory to instances of Singleton

(2). Call the Singleton constructor to create the object

(3). Point the Singleton object to the memory allocation space

However, since the Java compiler allows the processor to execute in order, and the cache, register, and main memory write-back order in the JVM before JDK1.5, (2) and (3) are not fixed,

So maybe the order of execution is 123, or maybe 132.

If it is 132, then this time, such as a thread execution to 13, has not executed 2, at this time the B thread is also executing, check singleton non-empty, direct use, will be wrong

If it is a JDK1.5 and a later version, add the volatile keyword:

Private volatile Static Singleton Singleton;

This ensures that the singleton object is read from the main memory every time.

Static internal class Singleton mode:

1  Public classsingleton{2     Privatesingleton{}3      Public StaticSingleton getinstance () {4         returnsingletonholder.sinstance;5     }6     /*7 static inner class8     */9     Private Static classsingletonholder{Ten         Private Static FinalSingleton sinstance =NewSingleton (); One     } A}

Pros: Only the first call to GetInstance will cause sinstance to be initialized, and the first call will cause the virtual machine to load the Singletonholder class,

This ensures thread safety, ensures that singleton objects are unique, and delays the instantiation of a single case

Enumeration Single Example:

1  Public enum singletonenum{2    INSTANCE; 3 }

Pros: Thread-safe, unique, and prevent deserialization.

Unique for destroying singleton objects:

1. Reflection

2. Serialization and deserialization

http://www.hollischuang.com/archives/1144

"Reading" Two, 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.