java2020年面试单例模式实现的5中方式比较

第一种:饿汉模式最简单的方式:安全


/*
 * 饿汉式:
 * 	在类初始化时直接创建实例对象,不管你是否需要这个对象都会创建
 * 
 * (1)构造器私有化
 * (2)自行创建,并且用静态变量保存
 * (3)向外提供这个实例
 * (4)强调这是一个单例,我们可以用final修改
 */
public class Singleton1 {
	public static final Singleton1 INSTANCE = new Singleton1();
	private Singleton1(){
		
	}
}

检验:


public class TestSingleton1 {

	public static void main(String[] args) {
		Singleton1 s = Singleton1.INSTANCE;
		System.out.println(s);
	}

}
/****
 * 运行结果
 
Singleton1@15db9742
*/

第二种:枚举类型线程安全

/*
 * 枚举类型:表示该类型的对象是有限的几个
 * 我们可以限定为一个,就成了单例
 */
public enum Singleton2 {
	INSTANCE
}

 检验代码:

public class TestSingleton2 {

	public static void main(String[] args) {
		Singleton2 s = Singleton2.INSTANCE;
		System.out.println(s);
	}

}
/*****
INSTANCE
*/

第三种 功能说明: 读取single.properties数据生成单例安全

/****
 * 
 * 功能说明: 读取single.properties数据生成单例
 *
 * @date 2020年2月23日
 * @author 
 * @email [email protected]
 *
 */
public class Singleton3 {
	public static final Singleton3 INSTANCE;
	private String info;
	
	static{
		try {
			Properties pro = new Properties();
			
			pro.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
			
			INSTANCE = new Singleton3(pro.getProperty("info"));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	private Singleton3(String info){
		this.info = info;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	@Override
	public String toString() {
		return "Singleton3 [info=" + info + "]";
	}
	
}

 实验结果检验:


public class TestSingleton3 {

	public static void main(String[] args) {
		Singleton3 s = Singleton3.INSTANCE;
		System.out.println(s);
	}

}
/***
*
Singleton3 [info=com.single]

*/

第四种懒汉式:没有优化前,不是线程安全模式


/*
 * 懒汉式:
 * 	延迟创建这个实例对象
 * 
 * (1)构造器私有化
 * (2)用一个静态变量保存这个唯一的实例
 * (3)提供一个静态方法,获取这个实例对象
 */
public class Singleton4 {
	private static Singleton4 instance;
	private Singleton4(){
		
	}
	public static Singleton4 getInstance(){
		if(instance == null){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			instance = new Singleton4();
		}
		return instance;
	}
}

检验结果:


public class TestSingleton4 {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
/*		Singleton4 s1 = Singleton4.getInstance();
		Singleton4 s2 = Singleton4.getInstance();
	
		System.out.println(s1 == s2);
		System.out.println(s1);
		System.out.println(s2);*/
		
		Callable<Singleton4> c = new Callable<Singleton4>() {

			@Override
			public Singleton4 call() throws Exception {
				return Singleton4.getInstance();
			}
		};
		
		ExecutorService es = Executors.newFixedThreadPool(2);
		Future<Singleton4> f1 = es.submit(c);
		Future<Singleton4> f2 = es.submit(c);
		
		Singleton4 s1 = f1.get();
		Singleton4 s2 = f2.get();
		
		System.out.println(s1 == s2);
		System.out.println(s1);
		System.out.println(s2);
		
		es.shutdown();
		
	}

}
/***
运行结果:
false
com.single.Singleton4@33909752
com.single.Singleton4@55f96302
 */


第五种懒汉式单例模式进行优化,线程安全


/*
 * 懒汉式:
 * 	延迟创建这个实例对象
 * 
 * (1)构造器私有化
 * (2)用一个静态变量保存这个唯一的实例
 * (3)提供一个静态方法,获取这个实例对象
 */
public class Singleton5 {
	private static Singleton5 instance;
	private Singleton5(){
		
	}
	public static Singleton5 getInstance(){
		if(instance == null){
			synchronized (Singleton5.class) {
				if(instance == null){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					instance = new Singleton5();
				}
			}
		}
		return instance;
	}
}

运行结果检验:

public class TestSingleton5 {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		Callable<Singleton5> c = new Callable<Singleton5>() {

			@Override
			public Singleton5 call() throws Exception {
				return Singleton5.getInstance();
			}
		};
		
		ExecutorService es = Executors.newFixedThreadPool(2);
		Future<Singleton5> f1 = es.submit(c);
		Future<Singleton5> f2 = es.submit(c);
		
		Singleton5 s1 = f1.get();
		Singleton5 s2 = f2.get();
		
		System.out.println(s1 == s2);
		System.out.println(s1);
		System.out.println(s2);
		
		es.shutdown();
		
	}

}
/****
 * 运行结果
true
com.single.Singleton5@33909752
com.single.Singleton5@33909752
 * 
 * 
 * 
 * 
 */

第六种模式是内部类:线程安全

/*
 * 在内部类被加载和初始化时,才创建INSTANCE实例对象
 * 静态内部类不会自动随着外部类的加载和初始化而初始化,它是要单独去加载和初始化的。
 * 因为是在内部类加载和初始化时,创建的,因此是线程安全的
 */
public class Singleton6 {
	private Singleton6(){
		
	}
	private static class Inner{
		private static final Singleton6 INSTANCE = new Singleton6();
	}
	
	public static Singleton6 getInstance(){
		return Inner.INSTANCE;
	}
}

附件:single.properties

#key=value
info=com.single
发布了307 篇原创文章 · 获赞 15 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/u011488009/article/details/104463995