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.
35 lines
873 B
35 lines
873 B
package com.msb.singleton;
|
|
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* @Author bingor
|
|
* @Date 2022-10-06 10:25
|
|
* @Description:
|
|
* 静态内部类方式
|
|
* JVM保证单例
|
|
* 加载外部类时不会加载内部类,这样可以实现懒加载
|
|
* @Version: 1.0
|
|
*/
|
|
public class Singleton07 {
|
|
|
|
private Singleton07() {}
|
|
|
|
private static class Singleton07Holder {
|
|
private static final Singleton07 INSTANCE = new Singleton07();
|
|
}
|
|
|
|
public static Singleton07 getInstance() {
|
|
return Singleton07Holder.INSTANCE;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
for (int i=0; i<100; i++) {
|
|
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
|
|
System.out.println(Singleton07.getInstance().hashCode());
|
|
}).start();
|
|
}
|
|
}
|
|
|
|
}
|