Common design mode -- single design mode

Keywords: Java

Single design mode

  • Hungry man
  • Slacker type
  • Double thread checking

Hungry man

public class Person {
    
    private static final Person person=new Person();//Constant can't be changed the first time this class is loaded
    private String  nameString;
    public String getNameString() {
        return nameString;
    }
    public void setNameString(String nameString) {
        this.nameString = nameString;
    }
    //Structure privatization
    private Person(){
        
    }
    public static Person getPerson(){
        
        return person;
    }
}

Slacker type

Note: thread safety problems

In multithreading, there may be multiple calls. The first thread is null, so it goes to the bottom. But in the initialization process, the next thread comes in, and the first initialization is not finished, so both threads are null

 

public class Person {
    
    private static Person person;
    private String  nameString;
    public String getNameString() {
        return nameString;
    }
    public void setNameString(String nameString) {
        this.nameString = nameString;
    }
    //Structure privatization
    private Person(){
    }
    public static Person getPerson(){//Judge by if--------------
        if (person==null) {
            person=new Person();
        }
        return person;
    }
}

resolvent:

synchronization

 

public class Person {
    
    private static Person person;
    private String  nameString;
    public String getNameString() {
        return nameString;
    }
    public void setNameString(String nameString) {
        this.nameString = nameString;
    }
    //Structure privatization
    private Person(){
    }
    public static synchronized Person getPerson(){//Lock up
        if (person==null) {
            person=new Person();
        }
        return person;
    }
}

 

 

The previous solution is not perfect, but there is thread safety in multithreading

public static synchronized Person getPerson(){//In this code, only person=new Person(); is executed once, so the entire method does not need to be synchronized for high efficiency
        if (person==null) {
            person=new Person();
        }
        return person;
    }

A key:

public static  Person getPerson(){
        if (person==null) {
            synchronized (Person.class) {
                person=new Person();
            }
            
        }
        return person;
    }

Double thread checking

It also involves thread safety. It is possible that two methods step into if judgment almost at the same time, but in the return process, the first one is not finished, and the second one may pass the lock, so it needs to be judged:

 

public class Person {
    private static Person person;
    private String  nameString;
    public String getNameString() {
        return nameString;
    }
    public void setNameString(String nameString) {
        this.nameString = nameString;
    }
    //Structure privatization
    private Person(){
    }
    public static  Person getPerson(){
        if (person==null) {
            synchronized (Person.class) {
                if (person==null) {
                    person=new Person();
                    
                }
            }
            
        }
        return person;
    }
}

Posted by omprakash on Tue, 31 Mar 2020 23:41:52 -0700