Several ways to implement Java singleton pattern
This is how many books write:
public class SingleTon1 { private SingleTon1(){ } private static SingleTon1 instance = null; public static SingleTon1 getInstance(){ if(instance == null){ instance = new SingleTon1(); } return instance; }}However, this is not written in actual development, because there is a serious problem: when multi-threaded concurrent access is accessed, multiple instances may be generated! !
Here are some commonly used methods:
1. Use synchronized keywords
package singleton; public class SingleTon1 { private SingleTon1(){ } private static SingleTon1 instance = null; //Solution 1 of multi-threading problem, but it is not efficient! Because every call will be locked! 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 very clever method, only when null is added, no public static SingleTon1 getInstance2(){ if(instance == null){ synchronized (object){ instance = new SingleTon1(); } } return instance; } } 2. Add 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 there is another judgment here! ! 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 a class created by a singleton static class Nested{ private Nested(){ } static Nested instance = new Nested(); }}The above is the commonly used singleton creation pattern:
Test test code:
package singleton; import singleton.SingleTon3.Nested; public class Test2 { public static void main(String[] args) { // TODO Auto-generated method stub Nested singleton; Myrunnable mm = new Myrunnable(); Myrunnable m1 = new Myrunnable(); Myrunnable2 m2 = new Myrunnable2(); new Thread(m1).start(); new Thread(m2).start(); if(m1.singleton == m2.singleton){ //It is the same System.out.println("is the same"); }else{ System.out.println("is not the same"); } }} class Myrunnable implements Runnable{ Nested singleton; @Override public void run() { // TODO Auto-generated method stub singleton = SingleTon3.getNested(); SingleTon3.print(); } } class Myrunnable2 implements Runnable{ Nested singleton; @Override public void run() { // TODO Auto-generated method stub singleton = SingleTon3.getNested(); SingleTon3.print(); } }Output:
It's the same
thread_id:11
thread_id:10
The above is a compilation of the Java singleton model information. We will continue to add relevant information in the future. Thank you for your support for this website!