SoFunction
Updated on 2025-03-11

Four ways to implement Android singleton mode

1. Hungry Man Style

public class SingletionStarving {

    private static final SingletionStarving mInstance = new SingletionStarving();

    private SingletionStarving() {

    }

    public static SingletionStarving getInstance() {
        return mInstance;
    }
}
  • The constructor is modified with private and cannot be accessed externally.
  • Initializes when a static object is declared
  • static keyword modification, static variable, stored in memory, only one piece of data.
  • The final keyword is initialized only once, so there is only one mInstance instance.

2. Lazy style

public class SingletionSlacker {

    private static SingletionSlacker mInstance;

    private  SingletionSlacker() {}

    public static synchronized SingletionSlacker getInstance() {
        if (mInstance == null) {
            mInstance = new SingletionSlacker();
        }
        return mInstance;
    }
}
  • The constructor is modified with private and cannot be accessed externally.
  • Initialization is only done when using it when gettingInstance is called
  • static keyword modification, static variable, stored in memory, only one piece of data.
  • Synchronized thread-safe, uniqueness of singletons in multithreaded situation
  • Disadvantages: GetInstance will be synchronized once every time you call it, wasting resources

3. Double check locking method

The most recommended and used methods online

public class Singletion {

    private static Singletion mInstance;

    private Singletion() {}

    public static Singletion getmInstance() {
        if (mInstance == null) {
            synchronized () {                if (mInstance == null) {
                    mInstance = new Singletion ();                }
            }
        }
        return mInstance;
    }
}
  • The constructor is modified with private and cannot be accessed externally.
  • Initialization is only done when using it when gettingInstance is called
  • static keyword modification, static variable, stored in memory, only one piece of data
  • Synchronized thread-safe, uniqueness of singletons in multithreaded situation
  • Two times to judge empty, avoid multiple synchronizations (synchronized)

shortcoming

private static Singletion mInstance;
private Singletion() {}
public static Singletion getmInstance() {}

Due to the jvm feature, it allows for out-of-order execution, and the above three sentences are indefinitely ordered, so there may be invalid problems.
Step 1. If thread A executes getmInstance(), the construction method Singletion() has not been executed yet.
Step 2: At this time, thread B calls getmInstance(). Because A has executed getmInstance(), mInstance is not empty and is directly retrieved.
Step 3: Since B is directly obtained, and the real situation is that the thread construction method of A has not been executed yet, so mInstance is empty.
Although this situation is relatively small, it is also a situation. To solve this situation, java 1.6 began to add volatile keyword

private volatile static Singletion mInstance;

This avoids failure of the method. Although volatile consumes some performance, the best way to write

public class Singletion {

    private volatile static Singletion mInstance;
    private Singletion () {}
    public static Singletion getmInstance() {        if (mInstance == null) {
            synchronized () {                if (mInstance == null) {
                    mInstance = new Singletion();                }
            }
        }
        return mInstance;
    }
}

Although volatile makes the way perfect, the writing method without volatile keyword can basically meet most situations. Unless you want to run in high concurrency, or in code before java 1.6.

4. Static internal class method

public class SingletionInternalClass {

    private SingletionInternalClass() {}

    public static SingletionInternalClass getInstance() {
        return ;
    }

    private static class SingletionInternalClassHolder {
        private static final SingletionInternalClass instance = new SingletionInternalClass();
    }
}

The constructor is modified with private and cannot be accessed externally.

Initialization is only done when using it when gettingInstance is called

Call getInstance before returning to load the SingletionInternalClassHolder class, ensuring thread safety and ensuring the uniqueness of singletons

Summarize

The singleton model is implemented in which way, the core idea is the same
1. The constructor is privatized, and a unique instance is obtained through a static method.
2. Thread safety

Finally, it is recommended to use the double lock method and the static internal class method in the article to create a singleton pattern.

The above is the detailed content of the four implementation methods of Android singleton mode. For more information about the implementation of Android singleton mode, please pay attention to my other related articles!