//保证类只有一个实例,私有其构造器
private SingletonDemo01() {
}
//创建自身对象
private static SingletonDemo01 instance = new SingletonDemo01();
//全局公开的方法
public static SingletonDemo01 getInstance() {
return instance;
}
class SingletonDemo01Test {
public static void main(String[] args) {
SingletonDemo01 instance = SingletonDemo01.getInstance();
SingletonDemo01 instance2 = SingletonDemo01.getInstance();
System.out.println(instance.hashCode());
System.out.println(instance2.hashCode());
System.out.println(instance == instance2);
}
}
输出的结果为:
356573597
356573597
true
package com.xgp.company.第一种_单例模式.饿汉式;
/**
*
* 核心:保证一个类只有一个实例,并且提供一个范围该实例的全局访问点
*/
public class SingletonDemo01 {
//保证类只有一个实例,私有其构造器
private SingletonDemo01() {
}
//创建自身对象
private static SingletonDemo01 instance = new SingletonDemo01();
//全局公开的方法
public static SingletonDemo01 getInstance() {
return instance;
}
}
class SingletonDemo01Test {
public static void main(String[] args) {
SingletonDemo01 instance = SingletonDemo01.getInstance();
SingletonDemo01 instance2 = SingletonDemo01.getInstance();
System.out.println(instance.hashCode());
System.out.println(instance2.hashCode());
System.out.println(instance == instance2);
}
}
public class SingletonDemo02 {
//保证类只有一个实例,私有其构造器
private SingletonDemo02() {
}
//创建自身对象,当时不用立即加载
private static SingletonDemo02 instance;
//全局公开的方法 synchronized作用:加锁 多线程进来时会不安全,效率较低
public static synchronized SingletonDemo02 getInstance() {
if(instance == null) {
instance = new SingletonDemo02();
}
return instance;
}
}
public class SingletonDemo03 {
//保证类只有一个实例,私有其构造器
private SingletonDemo03() {
}
//创建自身对象,当时不用立即加载 volatile作用:尽大可能的解决极端情况的问题
private volatile static SingletonDemo03 instance;
//全局公开的方法 synchronized作用:加锁 多线程进来时会不安全,效率较低
public static SingletonDemo03 getInstance() {
if(instance == null) {
//定一次进来时加锁,后面进来时就不加锁了,提高了效率
synchronized (SingletonDemo03.class) {
if(instance == null) {
instance = new SingletonDemo03();
}
}
}
return instance;
}
}
public class SingletonDemo03 {
//破坏两次都用反射创建对象
private static boolean flag = false;
//保证类只有一个实例,私有其构造器
private SingletonDemo03() {
//防治被反射
synchronized (SingletonDemo03.class) {
if(flag == false) {
flag = true;
}else {
throw new RuntimeException("不要试图用反射破坏单例");
}
}
}
//创建自身对象,当时不用立即加载 volatile作用:尽大可能的解决极端情况的问题
private volatile static SingletonDemo03 instance;
//全局公开的方法 synchronized作用:加锁 多线程进来时会不安全,效率较低
public static SingletonDemo03 getInstance() {
if(instance == null) {
//定一次进来时加锁,后面进来时就不加锁了,提高了效率
synchronized (SingletonDemo03.class) {
if(instance == null) {
instance = new SingletonDemo03();
}
}
}
return instance;
}
}
class SingletonDemo03Test {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
/*
SingletonDemo03 instance1 = SingletonDemo03.getInstance();
SingletonDemo03 instance2 = SingletonDemo03.getInstance();
System.out.println(instance1 == instance2);
*/
Class<SingletonDemo03> clazz = SingletonDemo03.class;
//反射破坏单例
Constructor<SingletonDemo03> declaredConstructor = clazz.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true);
SingletonDemo03 instance1 = declaredConstructor.newInstance();
//破坏flag
Field flag = clazz.getDeclaredField("flag");
flag.setAccessible(true);
flag.set(clazz,false);
System.out.println(flag.get(clazz));
SingletonDemo03 instance2 = declaredConstructor.newInstance();
System.out.println(instance1 == instance2);
System.out.println(instance1.hashCode());
System.out.println(instance2.hashCode());
}
}
运行结果:
false
false
21685669
2133927002
public class SingletonDemo04 {
private SingletonDemo04() {
//防治被反射
synchronized (SingletonDemo04.class) {
if(InnerClass.instance != null) {
throw new RuntimeException("不要试图用反射破坏单例");
}
}
}
private static class InnerClass {
private static final SingletonDemo04 instance = new SingletonDemo04();
}
public static SingletonDemo04 getInstance() {
return InnerClass.instance;
}
}
@CallerSensitive
public T newInstance(Object ... initargs)
throws InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException
{
if (!override) {
if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, clazz, null, modifiers);
}
}
if ((clazz.getModifiers() & Modifier.ENUM) != 0)
throw new IllegalArgumentException("Cannot reflectively create enum objects");
ConstructorAccessor ca = constructorAccessor; // read volatile
if (ca == null) {
ca = acquireConstructorAccessor();
}
@SuppressWarnings("unchecked")
T inst = (T) ca.newInstance(initargs);
return inst;
}
/**
* 反射不能破坏枚举类型,枚举类纯天然的单例,最简单
*/
public enum SingletonDemo05 {
INSTANCE;
public SingletonDemo05 getInstance() {
return INSTANCE;
}
public String hello() {
return "Hello World!";
}
}
class SingletonDemo05Test {
public static void main(String[] args) {
SingletonDemo05 instance1 = SingletonDemo05.INSTANCE;
SingletonDemo05 instance2 = SingletonDemo05.INSTANCE.getInstance();
System.out.println(instance1 == instance2);
String hello = SingletonDemo05.INSTANCE.hello();
System.out.println(hello);
}
}
原文:https://www.cnblogs.com/xgp123/p/12262160.html