Java Singleton mode and java Singleton Mode
The Singleton mode is written in many books as follows:
public class SingleTon1 {private SingleTon1(){}private static SingleTon1 instance = null;public static SingleTon1 getInstance(){if(instance == null){instance = new SingleTon1();} return instance;}}
But it is not written in actual development. Because of a serious problem, multiple instances may be generated during multi-thread concurrent access !!
The following describes several common methods:
1. Use the synchronized keyword
Package singleton; public class SingleTon1 {private SingleTon1 () {} private static SingleTon1 instance = null; // multithreading solution 1, but the efficiency is not high! Because every call locks! Public static synchronized SingleTon1 getInstance () {if (instance = null) {instance = new SingleTon1 ();} return instance;} public void print () {System. out. println ("thread_id:" + Thread. currentThread (). getId ();} private static Object object = new Object (); // a clever method. The public static SingleTon1 getInstance2 () is not added after the lock is null () {if (instance = null) {synchronized (object) {instance = new SingleTon1 () ;}} return instance ;}}
2. Lock
Package singleton; import java. util. concurrent. locks. reentrantLock; public class SingleTon2 {private SingleTon2 () {} private static ReentrantLock lock = new ReentrantLock (); private static SingleTon2 instance = null; public void print () {System. out. println ("thread_id:" + Thread. currentThread (). getId ();} public static SingleTon2 getInstance2 () {if (instance = null) {lock. lock (); if (instance = null) {// note that You have to determine !! Instance = new SingleTon2 () ;}lock. unlock () ;}return instance ;}}
3. Use static variables:
Package singleton; public class SingleTon3 {public static void print () {System. out. println ("thread_id:" + Thread. currentThread (). getId ();} public static Nested getNested () {return Nested. instance;} // This is the class static class Nested {private Nested () {} static Nested instance = new Nested () ;}} created by the single instance ();}}
The above is a common mode for creating a singleton:
Test code:
Package singleton; import singleton. singleTon3.Nested; public class Test2 {public static void main (String [] args) {// TODO Auto-generated method stubNested singleton; Myrunnable mm = new Myrunnable (); myrunnable m1 = new Myrunnable (); Myrunnable 2 m2 = new Myrunnable2 (); new Thread (m1 ). start (); new Thread (m2 ). start (); if (m1.singleton = m2.singleton) {// is the same System. out. println ("the same");} else {System. out. println ("not the same") ;}} class Myrunnable implements Runnable {Nested singleton; @ Overridepublic void run () {// TODO Auto-generated method stubsingleton = SingleTon3.getNested (); singleTon3.print () ;}} class Myrunnable2 implements Runnable {Nested singleton; @ Overridepublic void run () {// TODO Auto-generated method stubsingleton = notify (); SingleTon3.print ();}}
Output:
Is the same
Thread_id: 11
Thread_id: 10