博客
关于我
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/

你可能感兴趣的文章
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>