Learning android -10 singleton

Example roughly divided into two kinds of single-mode, mode and starving lazy mode.

First, hungry man mode

Running on a system to create objects, do not think too much about the problem, it may be created many times, there are multiple objects. But obviously there will be a waste of memory. Thread Safety

public class Singleton{
    //类加载时就初始化
    private static final Singleton instance = new Singleton();
    
    private Singleton(){}

    public static Singleton getInstance(){
        return instance;
    }
}

Second, lazy mode

When only get to create instances, but is thread safe in.

public class Singleton2 {
 
	private static Singleton2 singleton;
	
	private Singleton2(){
		
	}
	
	public static synchronized Singleton2 getInstance(){
		if(singleton == null) 
			singleton = new Singleton2();
		return singleton;
	}
}

Third, the double-pattern lock mode lazy

public class Singleton3 {
	private volatile  static Singleton3 singleton;//1
	
	private Singleton3(){
		
	}
	
	public static Singleton3 getInstance(){
		if(singleton == null){ //2
			synchronized(Singleton3.class){ //3
				if(singleton == null){ //4
					singleton = new Singleton3();
				}
			}
		}
		return singleton;
	}
}

It can be divided into two places, 1,234

1, Step 1

Because singleton = new Singleton3 (); essentially non-atomic operations , in fact, in the JVM probably do three things.

1. allocate memory to the singleton

2. Call Singleton3 construction completed initialization

3. To make the singleton object reference pointer to the allocated memory space (this is done the singleton is not null)

However, in the present instant compiler JVM instruction reordering optimization. That is the above second and third steps in the order is not guaranteed, the final order of execution may be may be 1 1-2-3 3-2. If the latter, then finished in 3, 2 is not performed before, two threads being seized, then the singleton has a non-null (but not initialized), the two thread returns directly singleton, then and error.

Note: volatile blocking is not singleton = new Singleton () inside this sentence [1-2-3] instruction rearrangement, but to ensure the operation in a write ([1-2-3]) done before, not call read (if (instance == null)).

2, step 234

Analyzing inner layer: instantiating a plurality of times to prevent
the outer Analyzing: trying to think of a situation, when the thread has completed inner layer 1 is determined, the object is instantiated, the thread 3 also getInstace function calls, determining if there is no increase of the outer thread 3 or to continue waiting for completion of the thread 2, and the outer layer is determined together, there is no need to wait, return directly instantiated objects.

Fourth, the lazy mode memory leaks

In android, the activity will sometimes be used as a single-mode embodiment, so that there will be along the following lines

  public class Singleton3 {
    	private volatile  static Singleton3 singleton;//1
    	
    	private Singleton3(){
    		 this.mContext = mContext;
    	}
    	
    	public static Singleton3 getInstance(){
    		if(singleton == null){ //2
    			synchronized(Singleton3.class){ //3
    				if(singleton == null){ //4
    					singleton = new Singleton3(context);
    				}
    			}
    		}
    		return singleton;
    	}
    }

Where the context is usually passed in activity, Service and other contexts, which can lead to memory leaks.
Because when we exit the Activity, the Activity no use, but because singleton as a static Singleton (presence throughout the life cycle of the application) will continue to hold a reference to this Activity, leading to the release of Activity objects can not be recovered, they resulting in a memory leak.
Modification method, the context parameters to the global context:

private Singleton3(){
        		 this.mContext =mContext.getApplicationContext();
        	}

Reference:
https://blog.csdn.net/Imobama/article/details/81093394

Published 57 original articles · won praise 3 · Views 6210

Guess you like

Origin blog.csdn.net/qq_39830579/article/details/100516883