七、并发编程之单例问题与线程安全性深入解析

1、饿汉式

出现线性安全性问题要满足三个条件:多线程的环境下(满足)、必须有共享资源(满足)、对资源进行非原子性操作(不满足),没有线程安全性问题。

public class Singleton {
	//私有化构造方法
	private Singleton() {}
	//饿汉式
	private static Singleton instance = new Singleton();
	
	public static Singleton getInstance() {
		return instance;
	}
	
	public static void main(String[] args) {
		Singleton s1 = Singleton.getInstance();
		Singleton s2 = Singleton.getInstance();
		Singleton s3 = Singleton.getInstance();
		Singleton s4 = Singleton.getInstance();
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		System.out.println(s4);
	}
}

2、懒汉式

出现线性安全性问题要满足三个条件:多线程的环境下(满足)、必须有共享资源(满足)、对资源进行非原子性操作(满足),有线程安全性问题。

public class Singleton2 {
	//私有化构造方法
	private Singleton2() {}
	//懒汉式
	private static Singleton2 instance;
	
	public static Singleton2 getInstance() {
		if(instance==null) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			instance = new Singleton2();
		}
		return instance;
	}
	
	public static void main(String[] args) {
		//单线程
//		Singleton2 s1 = Singleton2.getInstance();
//		Singleton2 s2 = Singleton2.getInstance();
//		Singleton2 s3 = Singleton2.getInstance();
//		Singleton2 s4 = Singleton2.getInstance();
//		System.out.println(s1);
//		System.out.println(s2);
//		System.out.println(s3);
//		System.out.println(s4);
		
		//多线程
		ExecutorService threadPool = Executors.newFixedThreadPool(20);//固定线程池
		for(int i=0;i<20;i++) {
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName()+Singleton2.getInstance());
				}
			});
		}
		threadPool.shutdown();
	}
}

解决方法:使用volatile避免指令重排序产生的线程安全性问题,使用synchronized代码块双重检查加锁制

public class Singleton2 {
	//私有化构造方法
	private Singleton2() {}
	//懒汉式  volatile避免指令重排序产生的线程安全性问题
	private static volatile Singleton2 instance;

	/**
	 * 为了提高程序的性能,不要直接在方法上加synchronized
	 * 使用synchronized代码块双重检查加锁制
	 * */
	public static Singleton2 getInstance() {
		//自旋
		if(instance==null) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized(Singleton2.class){
				if(instance==null) {//双重检查加锁制
					instance = new Singleton2();
					/**
					 * 指令重排序,也可能会造成安全问题
					 * 正常情况下是按照正常步骤执行,但是出现指令重排序的时候,就可能会打乱步骤执行
					 * 1.申请一块内存空间
					 * 2.在这快空间里实例化对象
					 * 3.instance的引用指向这块空间的地址
					 * 这个适合只要使用volatile关键字避免指令重排序产生的线程安全性问题
					 * */
				}
				
			}
		}
		return instance;
	}

	public static void main(String[] args) {
		//单线程
		//		Singleton2 s1 = Singleton2.getInstance();
		//		Singleton2 s2 = Singleton2.getInstance();
		//		Singleton2 s3 = Singleton2.getInstance();
		//		Singleton2 s4 = Singleton2.getInstance();
		//		System.out.println(s1);
		//		System.out.println(s2);
		//		System.out.println(s3);
		//		System.out.println(s4);

		//多线程
		ExecutorService threadPool = Executors.newFixedThreadPool(20);//固定线程池
		for(int i=0;i<20;i++) {
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName()+Singleton2.getInstance());
				}
			});
		}
		threadPool.shutdown();
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值