Implementation data collation of Java singleton pattern

  • 2020-05-12 02:34:55
  • OfStack

The implementation of Java singleton pattern is summarized in this paper. The implementation methods of java singleton pattern are summarized as follows:

This is what the singleton pattern says in many books:


public class SingleTon1 {
   
  private SingleTon1(){ 
  }
 
  private static SingleTon1 instance = null;
 
   
  public static SingleTon1 getInstance(){
    if(instance == null){
      instance = new SingleTon1();
    }
    return instance;
  }
}

But the actual development is not so write, because there is a serious problem, multi-threaded concurrent access, may produce multiple instances!!

Here are some common methods:

1. Use the synchronized keyword


package singleton;
 
public class SingleTon1 {
   
   
  private SingleTon1(){
     
  }
 
  private static SingleTon1 instance = null;
   
  // Multithreaded problem solving 1 But not very efficient! Because every call is 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();
  // Very clever method, only in null Lock it in, then leave it out 
  public static SingleTon1 getInstance2(){
     
    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){ // Notice here also to judge!! 
        instance = new SingleTon2();
      }
      lock.unlock();
    }
    return instance;
  }
}

3. Use of 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 created by the singleton 
  static class Nested{
   private Nested(){
    }
  static Nested instance = new Nested();
  }
}

This is a common pattern for creating singletons:

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){ // Is the same 1 a 
      System.out.println(" Is the same 1 a ");
    }else{
      System.out.println(" Is not the same 1 a ");
    }
   }
}
  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:

Is the same one

thread_id:11
thread_id:10

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: