Home  >  Article  >  Java  >  What is the difference between java lazy mode and hungry mode?

What is the difference between java lazy mode and hungry mode?

WBOY
WBOYforward
2023-05-03 20:43:051591browse

Difference

1. The lazy man is lazy and only goes back to initialize the singleton when getInstance is called.

2. Once the class is loaded, the singleton initialization is completed to ensure that the singleton already exists when getInstance is used.

Thread safety:

Hungry The Chinese style is inherently thread-safe and can be directly used for multi-threading without any problems.

The lazy style itself is not thread-safe (double check lock to solve concurrency problems)

Resource loading and performance:

Hungry Chinese style instantiates a static object when the class is created. Regardless of whether the singleton is used in the future, it will occupy a certain amount of memory, but accordingly, since its resources have been initialized, the speed of the first call It will also be faster.

Lazy style will delay loading. The instance object will only appear when this singleton is used for the first time. It needs to be initialized when called for the first time. If there is a lot of work to be done, the performance will be delayed. From now on it will be like hungry Chinese style.

2. Example

public class SingleTon01 {
    public static void main(String[] args) {
        GirlFriend gf1 = GirlFriend.getGf();
        GirlFriend gf2 = GirlFriend.getGf();
        //true
        System.out.println(gf1 == gf2);
 
        Cat cat1 = Cat.getCat();
        Cat cat2 = Cat.getCat();
        //true
        System.out.println(cat1 == cat2);
    }
}
 
 
/**
 * 单例模式-饿汉模式
 * 在类加载的时候就会创建对象
 */
class GirlFriend {
    public String name;
 
    private static GirlFriend gf = new GirlFriend("小红");
 
    public static GirlFriend getGf() {
        return gf;
    }
 
    /**
     * 构造器私有化,不能在本类之外new
     * @param name
     */
    private GirlFriend(String name) {
        this.name = name;
    }
 
    @Override
    public String toString() {
        return "GirlFriend{" +
                "name='" + name + '\'' +
                '}';
    }
}
 
/**
 * 单例模式-懒汉模式
 * 1.构造器私有化
 * 2.提供一个static静态属性对象
 * 3.提供一个public的static方法,返回一个实例对象
 * 4.懒汉模式,只有在用户调用方法时,才会创建对象,之后再次调用,返回的是同一对象
 */
class Cat {
    private String name;
 
    private static Cat cat;
 
    private Cat(String name) {
        this.name = name;
    }
 
    public static Cat getCat() {
        if (cat == null) {
            cat = new Cat("加菲猫");
        }
        return cat;
    }
 
    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                '}';
    }
}

The above is the detailed content of What is the difference between java lazy mode and hungry mode?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete