You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tank/src/main/java/com/msb/singleton/Singleton01.java

31 lines
806 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.msb.singleton;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
* 饿汉式
* 类加载到内存后就实例化一个单例JVM保证线程安全
* 简单实用,推荐使用!
* 唯一缺点:不管用到与否,类加载是就完成了实例化
* (话说你不用的,你装载它干啥)
* @Version: 1.0
*/
public class Singleton01 {
private static final Singleton01 INSTANCE = new Singleton01();
private Singleton01() {}
public static Singleton01 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton01 singleton1 = Singleton01.getInstance();
Singleton01 singleton2 = Singleton01.getInstance();
System.out.println(singleton1 == singleton2);
}
}