Package test;
Public class tiger6 {static class inner {private static inner instance = null; private inner () {// Per Call a message, print a message. System.out.println ("Create A Inner Object!");} Public static inner getinstance () {if (instance == null) {instance = new inner ();} return instance;}} [] args) {thread [] TS = new thread [[100]; for (int i = 0; i result: Create a Inner Object! Create a inner object! Create a inner Object! Obviously, the result is a violation of the single sample requirements. Synchronized is now added in the Inner.GetInstance () method. result: Create a inner Object! Of course there is another approach: Public static inner getinstance () { Synchronized (inner.class) { IF (instance == null) {instance = new inner (); } Return Instance;} Analysis: Through the above approach, it can be controlled on the same time, only one thread is allowed to access the variable instance. Return Instance;} Analysis: Through the above approach, it can be controlled on the same time, only one thread is allowed to access the variable instance. Of course, there is only this problem with the lazy single case model. For hungry-style single case mode, the Inner object is created when loading is loaded.