Public class Singleton {
Private static Singleton instance = null; private Singleton () {} private static synchronized void syncInit () {if (instance = null) {instance = new Singleton ();}} public static Singleton getInstance () {if (instance = null) {syncInit ();} return instance;} application: Generally speaking: The Singleton mode is generally in, you only want to use one thing and cannot use it when adding it. For example, in QQ chat, you only need to open a dialog box, no matter how many times the friend is clicked or the first window, to control resource usage, use thread synchronization to control concurrent resource access. Second, control the number of instances generated to save resources. Third, it is used as a communication media, that is, data sharing. It can enable communication between multiple unrelated two threads or processes without establishing a direct association. For example, the database connection pool is generally designed in the singleton mode. database connection is a type of database resource.