package simpleMode;
/*
* 单利模式:
* 保证一个类仅有一个实例,并提供一个访问他的全局访问点
*
* 通常我们可以让一个全局变量使得一个对象被访问,但它不能防止你实例化多个对象,
* 一个最好的方法就是让类自身保存他的对象实例,这个类可以保证没有其他实例被创建,
* 并且他可以提供一个访问该实例的方法
*
*/
public class SimpleMode {
public static void main(String[] args) {
/*//懒汉式 模拟线程不安全
new Thread(){
public void run() {
System.out.println("线程不安全:"+Singleton1.getInstance().hashCode());
}
}.start() ;
new Thread(){
public void run() {
System.out.println("线程不安全:"+Singleton1.getInstance().hashCode());
}
}.start() ;
//2次的hashCode不一样 说明创建了2个实例 这就是2个线程同时进入了 if (instance == null)
//所以导致了问题的出现 第一个线程判断为空之后创建一个实例 第二个线程如果后进去的话 是不为空的 但是
//他是和第一个线程一起进入到if (instance == null) 的 在他进入的时候 判断为空是true


//懒汉式 线程安全
//可以看到 线程安全打印出来的hashCode是相同的
//为什么线程安全呢?
//因为在getInstance方法上加了一个synchronized修饰符 多个线程不能同时访问次方法
//第二个线程要等第一个线程访问完 这个方法之后才能访问这个方法 这个时候instance就不是null了 所以就不会重新new 对象了
//这种方式很少用 因为synchronized 是程序的瓶颈
new Thread(){
public void run() {
System.out.println("线程安全:"+Singleton2.getInstance().hashCode());
}
}.start() ;
new Thread(){
public void run() {
System.out.println("线程安全:"+Singleton2.getInstance().hashCode());
}
}.start() ;*/

//饿汉模式测试 2次输出是一样的
/*System.out.println( Singleton3.getInstance().hashCode());
System.out.println( Singleton3.getInstance().hashCode());*/

//枚举类型测试
/*System.out.println(Singleton6.INSTANCE.getInstance().hashCode());
System.out.println(Singleton6.INSTANCE.getInstance().hashCode());*/
}
}



/*
* 懒汉,线程不安全
* 懒汉式的特点是延迟加载,比如配置文件,采用懒汉式的方法,顾名思义,懒汉么,很懒的,配置文件的实例直到用到的
*/
class Singleton1 {
private static Singleton1 instance;
private Singleton1 (){}
public static Singleton1 getInstance() {
if (instance == null) {
//为了达到效果 这里sleep2秒 在高并发的情况下
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
instance = new Singleton1();
}
return instance;
}

}
/*
* 懒汉,线程安全
*/
class Singleton2{
private static Singleton2 instance;
private Singleton2 (){}
public synchronized static Singleton2 getInstance() {
if (instance == null) {
//为了达到效果 这里sleep2秒 在高并发的情况下
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
instance = new Singleton2();
}
return instance;
}

}


/*
*饿汉式:
*饿汉式的特点是一开始就加载了,
*如果说懒汉式是“时间换空间”,那么饿汉式就是“空间换时间”,因为一开始就创建了实例,所以每次用到的之后直接返回就好了
*这种方式基于classloder机制避免了多线程的同步问题,
*不过,instance在类装载时就实例化,虽然导致类装载的原因有很多种,
*在单例模式中大多数都是调用getInstance方法,
* 但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,
* 这时候初始化instance显然没有达到lazy loading的效果。
*/
class Singleton3 {
private static Singleton3 instance = new Singleton3();
private Singleton3 (){
System.out.println("11111111111");
}
public static Singleton3 getInstance() {
return instance;
}
}

/*
* 饿汉式 换汤不换药 跟上边一样
*/
class Singleton4 {
private static Singleton4 instance = null;
static {
instance = new Singleton4();
}
private Singleton4 (){}
public static Singleton4 getInstance() {
return instance;
}
}


/*
*静态内部类
*这种方式同样利用了classloder的机制来保证初始化instance时只有一个线程,
*它跟第三种和第四种方式不同的是(很细微的差别):第三种和第四种方式是只要Singleton类被装载了,
*那么instance就会被实例化(没有达到lazy loading效果),
*而这种方式是Singleton类被装载了,instance不一定被初始化。
*因为SingletonHolder类没有被主动使用,只有显示通过调用getInstance方法时,
*才会显示装载SingletonHolder类,从而实例化instance。
*想象一下,如果实例化instance很消耗资源,我想让他延迟加载,
*另外一方面,我不希望在Singleton类加载时就实例化,因为我不能确保Singleton类还可能在其他的地方被主动使用从而被加载,
*那么这个时候实例化instance显然是不合适的。这个时候,这种方式相比第三和第四种方式就显得很合理。
*/
class Singleton5 {
private static class SingletonHolder {
private static final Singleton5 INSTANCE = new Singleton5();
}
private Singleton5 (){}
public static final Singleton5 getInstance() {
return SingletonHolder.INSTANCE;
}
}

/*
* 枚举模式
* 建议先看看关于枚举的相关知识
* 首先,在枚举中我们明确了构造方法限制为私有,在我们访问枚举实例时会执行构造方法,
* 同时每个枚举实例都是static final类型的,也就表明只能被实例化一次。
* 在调用构造方法时,我们的单例被实例化。
* 也就是说,因为enum中的实例被保证只会被实例化一次,所以我们的INSTANCE也被保证实例化一次。
* 可以看到,枚举实现单例还是比较简单的,
*
*
* 《Effective Java》作者比较推崇这种做法 他说:单元素的枚举类型已经成为实现Singleton的最佳方法。
*/
enum Singleton6 {
INSTANCE;
private Singleton77 instance;
Singleton6(){
instance = new Singleton77();
}
public Singleton77 getInstance() {
return instance;
}
}
class Singleton77{

}


/*
* 双重校验
* 这个是第二种方式的升级版,俗称双重检查锁定
* 这种方式使用比较多
* volatile关键字 可以查阅相关资料:
* 对于volatile修饰的变量,jvm虚拟机只是保证从主内存加载到线程工作内存的值是最新的
* 例如假如线程1,线程2 在进行read,load 操作中,发现主内存中count的值都是5, 那么都会加载这个最新的值
* 在线程1对count进行修改之后,会write到主内存中,主内存中的count变量就会变为6
* 线程2由于已经进行read,load操作,在进行运算之后,也会更新主内存count的变量值为6
* 导致两个线程及时用volatile关键字修改之后,还是会存在并发的情况。
*/
class Singleton8 {
private volatile static Singleton8 singleton;
private Singleton8 (){}
public static Singleton8 getSingleton() {
//这里可能同时有2个线程进入
if (singleton == null) {
//如果有多个线程进入 这里就会阻塞 但是这里基本没有线程进来 因为第一次初始化之后就不会进这里了
synchronized (Singleton8.class) {
if (singleton == null) {
singleton = new Singleton8();
}
}
}
return singleton;
}
}
//SimpleMode.java


/*
*枚举相关知识 这里有一个连接 讲的凑合
*/