博客
关于我
Java中的单例模式
阅读量:580 次
发布时间:2019-03-11

本文共 4099 字,大约阅读时间需要 13 分钟。

单例模式

饿汉式 和 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
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
declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class); declaredConstructor.setAccessible(true); EnumSingle instance2 = declaredConstructor.newInstance(); //java.lang.NoSuchMethodException: com.chao.single.EnumSingle.
() 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.javapackage 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        });    }}

转载地址:http://igrvz.baihongyu.com/

你可能感兴趣的文章
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>