java-based memory --java two singleton

/ *      
 * Java run the memory: 
 * heap (heap): all stored objects, each object contains information about a class corresponding thereto 
 * only one heap, shared by all threads, the heap does not store objects and basic data types reference, only the object itself put 
 * stack: each thread has its own stack (stack user mode), the stack of basic data types stored objects and custom objects references, examples of this reference point to the heap area 
 * area method (static area): shared by all threads, including all class and static variables 
 * includes forever the only element of the whole process, that class, static variables 
 * 
 * 
 * how to determine a property, method to determine as static 
 * attributes: can be multiple objects sharing, not with different objects and different 
 * methods: the method of operation static property 
 * methodological tools in the class, it is customary declared as static, such as:. the Math, Arrays, the Collections 
 * * /

Singleton

/ * 
 * Singleton 
 * Comparison: 
 * starving type: 
 * Benefits: thread-safe 
 * disadvantage: a long time to load     
 * lazy type: 
 * Benefits: The delay created 
 * disadvantage: thread-safe, there are concurrent objects are created 
 * 
 * single common Example scene mode: 
 * website counter application log application, database connection pool, read a configuration file class 
 * application, task Manager, Recycle Bin 
 * * /

Starving type, when it is loaded with the new class

/ * 
 * 1. starving the formula: a new start out 
 * * / 
class Bank {
     // 1. privatization constructor 
    Private Bank () {} 
    
    // static object class is created inside 2. 
    Private  static Bank instance = new Bank (); 
    
    // 3. providing public static method, returns the class object 
    public  static Bank the getInstance () {
         return instance; 
    } 
}

Lazy style

/ * 
 * 2. lazy type: What time to use when re- 
 * * / 
class the Order {
     // 1. Privatization constructor 
    Private the Order () {} 
    
    // 2. declare the current class static object is not initialized 
    Private  static the Order = instance null ; 
    
    // returns the class object 3. the method of statement public static 
    public  static the Order the getInstance () {
         IF (instance == null ) 
            instance = new new the Order ();
         return instance; 
    } 

}

Guess you like

Origin www.cnblogs.com/zsben991126/p/12148167.html