码迷,mamicode.com
首页 > 编程语言 > 详细

Java中的单例模式

时间:2021-05-24 08:22:34      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:有一个   data   lan   ESS   copy   eof   extend   异常   image   

单例模式

饿汉式 和 DCL懒汉式

饿汉式

package com.chao.single;
//饿汉式单例
public class Hungry {
    //可能会浪费空间
    private byte[] data1 = new byte[1024*1024];
    private byte[] data2 = new byte[1024*1024];
    private byte[] data3 = new byte[1024*1024];
    private byte[] data4 = new byte[1024*1024];

    //构造器私有,别人无法new,保持内存中只有一个对象
    private Hungry(){

    }
    //new 一个对象
    private final static Hungry HUNGRY = new Hungry();
    public static Hungry getInstance(){
        return HUNGRY;
    }
}

DCL 懒汉式

package com.chao.single;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
//懒汉式单例
//道高一尺,魔高一丈!
public class LazyMan {
    private static boolean chao = false;
    private LazyMan(){
        synchronized (LazyMan.class){
            if(chao == false){
                chao = true;
            }else{
                throw new RuntimeException("不要试图使用反射破坏异常");
            }
        }

    }
    private volatile static LazyMan lazyMan;
    //双重检测锁模式的 懒汉式单例  DCL懒汉式
    public static LazyMan getInstance(){
        if(lazyMan==null){
            synchronized (LazyMan.class){
                if(lazyMan==null){
                    lazyMan = new LazyMan(); //不是一个原子性操作
                }
            }
        }
        return lazyMan;
    }
    /**
     * 1. 分配内存空间
     * 2. 执行构造方法,初始化对象
     * 3. 把这个对象指向这个空间
     *
     *123
     * 132 A
     *     B //此时lazyMan还没有完成构造
     */
    //单线程下确实单例OK
    //多线程并发
    //反射!
    public static void main(String[] args) throws Exception {
//        for (int i = 0; i <10 ; i++) {
//            new Thread(()->{
//                LazyMan.getInstance();
//            }).start();
//
//        }
//        LazyMan instance = LazyMan.getInstance();
        Field chao = LazyMan.class.getDeclaredField("chao");
        chao.setAccessible(true);
        Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true);
        LazyMan instance = declaredConstructor.newInstance();
        chao.set(instance,false);
        LazyMan instance2 = declaredConstructor.newInstance();

        System.out.println(instance);
        System.out.println(instance2);

    }
}

静态内部类

package com.chao.single;
//静态内部类
public class Holder {
    private Holder(){
    }
    public static Holder getInstance(){
        return InnerClass.HOLDER;
    }
    public static class InnerClass{
        private static final Holder HOLDER = new Holder();
    }
}

单例不安全,反射

枚举

package com.chao.single;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//enum 是一个什么?本身也是一个Class类
public enum EnumSingle {

    INSTANCE;

    public EnumSingle getInstance(){
        return INSTANCE;
    }
}
class Test{
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        EnumSingle instance1 = EnumSingle.INSTANCE;
        Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class);
        declaredConstructor.setAccessible(true);
        EnumSingle instance2 = declaredConstructor.newInstance();
        //java.lang.NoSuchMethodException: com.chao.single.EnumSingle.<init>()
        System.out.println(instance1);
        System.out.println(instance2);

    }
}

技术图片

反编译 jad

技术图片

枚举类型的最终反编译 源码:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   EnumSingle.java
package com.chao.single;
public final class EnumSingle extends Enum
{
    public static EnumSingle[] values()
    {
        return (EnumSingle[])$VALUES.clone();
    }

    public static EnumSingle valueOf(String name)
    {
        return (EnumSingle)Enum.valueOf(com/chao/single/EnumSingle, name);
    }

    private EnumSingle(String s, int i)
    {
        super(s, i);
    }

    public EnumSingle getInstance()
    {
        return INSTANCE;
    }

    public static final EnumSingle INSTANCE;
    private static final EnumSingle $VALUES[];

    static 
    {
        INSTANCE = new EnumSingle("INSTANCE", 0);
        $VALUES = (new EnumSingle[] {
            INSTANCE
        });
    }
}

Java中的单例模式

标签:有一个   data   lan   ESS   copy   eof   extend   异常   image   

原文地址:https://www.cnblogs.com/Answer-Chao/p/14766181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!