*Lazy Type: (Multithreading high concurrent access to consider synchronization issues)>code, take the user entity as example (user) Publiucclassuser{/*1. User entity, static global dependency, class load is initialized to null pointer, no memory address, but also * is can reference*/ Private StaticUser instance=NULL; //2. Private constructors, external cannot have newly created objects in heap memory with the new keyword PrivateUser () {}//Lazy Load instantiated objects Public Static synchronizedUser Getinstace () {//if the instantiated object is a null pointer if(!instancre) { //instantiate the current objectInstance=NewUser (); returninstance; }Else{ //Otherwise, the currently instantiated object is returned returninstance; } } } *a hungry man type:>code, take the user entity as an example: Public classuser{//class loading is the initialization of the user object, a hungry man, starvation loading, Private StaticUser instance=NewUser (); //for external access interfaces, which are instantiated objects for external access Public StaticUser getinstance () {returninstance; } //private constructors, simulating databases PrivateUser () {}} Note: In singleton mode, only one instance of a class is promised, so the constructor is private. *lazy is not to initialize the class at the time of class loading instantiation object, can not guarantee the uniqueness of the object, so in multi-threaded case, despite the null value of the instance object, it will also lead to thread safety problem, because multithreading is to run the thread according to the time slice, destroy the class code of the order of vertical execution, because This plus the Sync keyword synchronized, is code block synchronization, thread safety. *a hungry man because the static class object is instantiated when the class is loaded, there is only one static global dependent class object at any time, and there is no security problem with threads.
Single-Case mode