package com.atguigu.test;
//懒汉需要再创建
class Sigleton1{
private static volatile Sigleton1 instance;
private Sigleton1(){}
public static Sigleton1 getInstance(){
//双重锁 线程安全
if(instance!=null)
return instance;
synchronized (Sigleton1.class){
if(instance==null)
{
instance = new Sigleton1();
}
}
return instance;
}
public void show(){
System.out.println("hello sigleton");
}
}
//恶汉 直接创建
class Sigleton2{
private static Sigleton2 instance = new Sigleton2();
private Sigleton2(){
}
public static Sigleton2 getInstance(){
return instance;
}
}
public class MySigleton {
public static void main(String[] args) {
System.out.println(Sigleton2.getInstance().equals(Sigleton2.getInstance()));
}
}