设计模式之单例模式

22 篇文章 0 订阅
1 篇文章 0 订阅

写了一个饿汉式和一个双重加锁的单例

饿汉式代码:

package com.freshbin.pattern.singleton;

/**
 * 单例模式 饿汉式
 * @author freshbin
 * @date 2019-1-3 19:44:05
 */
public class SingletonHunger {
	private static SingletonHunger singleton = new SingletonHunger();
	private static String flag;
	
	private SingletonHunger() {
		
	}
	
	public static SingletonHunger getSingleton(String i) {
		flag = i;
		return singleton;
	}
	
	public void showMsg() {
		System.out.println("第" + flag + "个线程调用单例打印信息!");
	}
}

双重加锁代码:

package com.freshbin.pattern.singleton;

/**
 * 单例模式 双重加锁
 * @author freshbin
 * @date 2019-1-3 19:43:01
 */
public class Singleton {
	private volatile static Singleton singleton;
	
	private Singleton() {
		
	}
	
	public static Singleton getSingleton(String threadName) {
		if(singleton == null) {
			synchronized(Singleton.class) {
				if(singleton == null) {
					System.out.println("线程" + threadName + "调用单例");
					singleton = new Singleton();
				}
			}
		}
		
		return singleton;
	}
	
	/*public void showMsg(Integer i) {
		System.out.println("第" + i + "个线程调用单例打印信息!");
	}*/
}

线程A:

package com.freshbin.pattern.singleton;

/**
 * 多线程调用单例
 * @author freshbin
 * @date 2019-1-3 19:55:45
 */
public class SingletonThreadA extends Thread {
	
	@Override
	public void run() {
		System.out.println("======线程A===双重加锁==========");
		for(int i = 0; i < 10; i++) {
			Singleton singleton = Singleton.getSingleton("A");
		}
		
		System.out.println("=====线程A====饿汉式==========");
		
		for(int i = 0; i < 10; i++) {
			SingletonHunger singleton = SingletonHunger.getSingleton("A" + i);
			singleton.showMsg();
		}
	}
}

线程B:

package com.freshbin.pattern.singleton;

/**
 * 多线程调用单例
 * @author freshbin
 * @date 2019-1-3 19:55:45
 */
public class SingletonThreadB extends Thread {
	
	@Override
	public void run() {
		System.out.println("======线程B===双重加锁==========");
		for(int i = 0; i < 10; i++) {
			Singleton singleton = Singleton.getSingleton("B");
		}
		
		System.out.println("=====线程B====饿汉式==========");
		
		for(int i = 0; i < 10; i++) {
			SingletonHunger singleton = SingletonHunger.getSingleton("B" + i);
			singleton.showMsg();
		}
	}
}

主类:

package com.freshbin.pattern.singleton;

/**
 * 单例模式主类
 * @author freshbin
 * @date 2019-1-3 19:51:45
 */
public class SingletonMain {
	public static void main(String[] args) {
		Thread aThread = new Thread(new SingletonThreadA());
		Thread bThread = new Thread(new SingletonThreadB());
		aThread.start();
		bThread.start();
	}
}

通过测试发现,双重加锁可以实现单例,而饿汉式的会出现多线程共享变量问题。

以下是效果图:

其中双重加锁的单例只调用了一次,说明锁起了作用;

而图中可以看到B0打印了两次,说明有一次其实是要打印A0的,只不过A0的值被B0覆盖了,所以出现了多线程问题。

github代码地址:https://github.com/freshbin/designPattern

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值