Features of single case mode and analysis of common single case mode (analysis of six single case modes)

Keywords: Java jvm Programming

Single case mode:

  1. That is to say, in the whole life cycle, the production of this object is always the same, unchanged.
  2. It ensures that a class has only one instance and provides a global access point to access it.

Effect:

  1. When thread safety is required, the uniqueness and thread safety of class instances are guaranteed.
  2. When multiple instances are not needed, the singleness of class instances is guaranteed. Don't waste memory.

Characteristic:

  1. The public method gets the instance,
  2. Private constructor,
  3. Private member variable.

 

1, Hungry Han style
*A single example of hungry Chinese style
* the key of starved Chinese singleton is that singleton is the class variable and is initialized directly, that is, all variables in the class will be initialized
* singleton as a class variable will be collected into < clinit > () method during initialization, which can ensure 100% synchronization,
* but because it is not lazy to load, singleton may not be used for a long time after being loaded, that is, the space opened by the instance will exist for a long time
* although the only instance of multithreading can be implemented, lazy loading cannot be performed;

package com.liruilong.singleton;
 
/**
 * @Author: Liruilong
 * @Date: 2019/7/20 17:55
 */
 
// final Inheritance not allowed
public final class Singleton {
    // Instance variables
    private byte[] bate = new byte[1024];
    // Private constructor, i.e. external is not allowed new
    private Singleton(){ }
    private  static final Singleton singleton1 = new Singleton();
    public static  Singleton getInstance1(){
        return singleton1;
    }

2, Slovenly

*Lazy singleton mode
* can guarantee lazy loading, but the thread is not safe
* when there are two threads accessing, the uniqueness of a single instance cannot be guaranteed

package com.liruilong.singleton;
 
/**
 * @Author: Liruilong
 * @Date: 2019/7/20 17:55
 */
 
// final Inheritance not allowed
public final class Singleton {
    // Instance variables
    private byte[] bate = new byte[1024];
    // Private constructor, i.e. external is not allowed new
    private Singleton(){ }
 
    private static  Singleton singleton =null;
public static Singleton getInstance(){ if (singleton == null) { singleton = new Singleton(); } return singleton; }

3, Lazy plus synchronization
*Lazy + synchronous method single instance mode
* both lazy loading and singleton instance uniqueness can be guaranteed, but the exclusive nature of synchronized keyword results in
* getInstance0() method can only be accessed by one thread at the same time. Poor performance.

 

package com.liruilong.singleton;
 
/**
 * @Author: Liruilong
 * @Date: 2019/7/20 17:55
 */
 
// final Inheritance not allowed
public final class Singleton {
    // Instance variables
    private byte[] bate = new byte[1024];
    // Private constructor, i.e. external is not allowed new
    private Singleton(){ }

    private static  Singleton singleton =null;
public static synchronized Singleton getInstance0(){ if (singleton == null) { singleton = new Singleton(); } return singleton; }

4, Single case of double effect lock
*Double check + volatile
* for the improvement of lazy synchronization method, when two threads find singleton null, only one thread can enter the synchronization code block.
* not only satisfies lazy loading, but also ensures the uniqueness of threads
* the disadvantage of not adding volume may sometimes report NPE (JVM running instruction reordering)
* it is possible that the variables of the instance object are not instantiated to get singleton variables from other threads.
* an instance that has not completed initialization calls its method and throws a null pointer exception.

 

package com.liruilong.singleton;
 
/**
 * @Author: Liruilong
 * @Date: 2019/7/20 17:55
 */
 
// final Inheritance not allowed
public final class Singleton {
    // Instance variables
    private byte[] bate = new byte[1024];
    // Private constructor, i.e. external is not allowed new
    private Singleton(){ }
 
    private  static volatile Singleton singleton2 = null;
public static Singleton getInstance4() { if (singleton2 == null){ synchronized (Singleton.class){ if (singleton2 ==null){ singleton2 = new Singleton(); } } } return singleton2; }

 

5, Single example of static internal class
* @ Description single instance mode of static inner class
* initialization of singleton class does not create singleton instance. Singleton instance is defined in static inner class.
* Singleton static variables will be created when a given static inner class is actively created, which is one of the best Singleton patterns
   

package com.liruilong.singleton;
 
/**
 * @Author: Liruilong
 * @Date: 2019/7/20 17:55
 */
 
// final Inheritance not allowed
public final class Singleton {
    // Instance variables
    private byte[] bate = new byte[1024];
    // Private constructor, i.e. external is not allowed new
    private Singleton(){ }
 
    private  static class Singtetons{
private static Singleton SINGLETON = new Singleton(); /* static { final Singleton SINGLETON = new Singleton(); }*/ } public static Singleton getInstance2(){ return Singtetons.SINGLETON; }

6, Enumeration class singleton

* @ Description based on enumeration class thread safety
* enumeration type cannot be inherited. It is also thread safe and can only be instantiated once.

 

package com.liruilong.singleton;
 
/**
 * @Author: Liruilong
 * @Date: 2019/7/20 17:55
 */
 
// final Inheritance not allowed
public final class Singleton {
    // Instance variables
    private byte[] bate = new byte[1024];
    // Private constructor, i.e. external is not allowed new
    private Singleton(){ }

    private enum Singtetonss {
        SINGTETONSS; //Instance must be in the first row, default public final static Modification
        private Singleton singleton;
 
        Singtetonss() { //The constructor. Default private
            this.singleton = new Singleton();
        }
        public static Singleton getInstance() {
            return SINGTETONSS.singleton;
        }
    }
    public static  Singleton getInstance3(){
        return Singtetonss.getInstance();
    }

 

It was originally from my notes. I picked it out and read it during the interview,

See more Reading notes on Java Concurrent Programming

Posted by elle_girl on Mon, 27 Jan 2020 04:46:28 -0800