Binder (Messenger) architecture and memory mapping for Android process communication

Why singletons and application scenarios

  1. Only one instance of a class is allowed in the entire program.
  2. Objects that need to be frequently instantiated and then destroyed.
  3. An object that takes too much time or resources to create but is often used.
  4. An environment that facilitates communication between resources,
  5. Encapsulate some common tool classes to ensure common application data consistency
  6. Store some shared data in memory that other classes can read at any time.
  7. In the case of resource sharing, avoid performance or loss caused by resource operations. As in the log files above, apply the configuration
  8. Facilitates communication between resources in the case of resource control. Such as thread pools.

Why use singleton we all clear nonsense do not say, directly on the code, disk it is over !!!!

Hungry

public class SingletionStarving { private static final SingletionStarving mInstance = new SingletionStarving(); private SingletionStarving() { } public static SingletionStarving getInstance() { return mInstance; }}Copy the code
  • Constructors are private and not externally accessible

  • Class when a static object is declared

  • Static variable, stored in memory, only one copy of data.

  • The final keyword is initialized only once, so there is only one mInstance instance.

Two. Slacker style

public class SingletionSlacker { private static SingletionSlacker mInstance; private SingletionSlacker() {} public static synchronized SingletionSlacker getInstance() { if (mInstance == null) { mInstance = new SingletionSlacker(); } return mInstance; }}Copy the code
  • Constructors are private and not externally accessible
  • It is initialized when getInstance is called
  • Static variable, stored in memory, only one copy of data.
  • Synchronized thread safety, the uniqueness of single cases in multithreading
  • Disadvantages: getInstance is synchronized every time, which wastes resources

Three. Double check lock method

Online suggestions and most used methods

public class Singletion { private static Singletion mInstance; private Singletion() {} public static Singletion getmInstance() { if (mInstance == null) { synchronized (Singletion.class) { if (mInstance == null) { mInstance = new Singletion (); } } } return mInstance; }}Copy the code
  • Constructors are private and not externally accessible
  • It is initialized when getInstance is called
  • Static variable, stored in memory, only one copy of data
  • Synchronized thread safety, the uniqueness of single cases in multithreading
  • Twice empty judgment, avoid synchronized

disadvantages

private static Singletion mInstance;
private Singletion() {}
public static Singletion getmInstance() {}
Copy the code

Due to the nature of the JVM, which allows out-of-order execution, the above three lines of code can be out of order, which can cause invalidation problems. If thread A executes getmInstance() but has not executed the constructor Singletion(), thread B calls getmInstance(). Since A has executed getmInstance(), mInstance is not empty. Step 3: mInstance is empty because B gets it directly, but the real situation is that THE thread constructor of A has not been executed yet. It’s less likely, but it’s a situation. To address this situation, java1.6 began adding the volatile keyword

private volatile static Singletion mInstance;
Copy the code

This avoids mode invalidation. Although volatile costs some performance, it is best written as volatile

public class Singletion { private volatile static Singletion mInstance; private Singletion () {} public static Singletion getmInstance() { if (mInstance == null) { synchronized (Singletion.class) { if (mInstance == null) { mInstance = new Singletion(); } } } return mInstance; }}Copy the code

While volatile makes the approach perfect, writing without the volatile keyword would suffice for most situations. Unless you’re running high concurrency, or java1.6 pre-code.

Static inner class mode

public class SingletionInternalClass { private SingletionInternalClass() {} public static SingletionInternalClass getInstance() { return SingletionInternalClassHolder.instance; } private static class SingletionInternalClassHolder { private static final SingletionInternalClass instance = new SingletionInternalClass(); }}Copy the code
  • Constructors are private and not externally accessible

  • It is initialized when getInstance is called

  • Call getInstance returned loaded SingletionInternalClassHolder class, make sure the thread safety, ensure the uniqueness of the singleton

conclusion

The singleton pattern doesn’t work that way, but the core idea is the same: 1. Privatize constructors and get a unique instance at a time using a static method. 2

Finally, it is recommended to create a singleton pattern using the ** double locking method and the ** static inner class method described in this article.

Thank you for your attention