单例的多线程线程安全问题的描述
通常的多线程的线程安全问题,往往被描述成"多线程共享线程实例变量"
这也证明了线程安全的本质是"实际值和理论值不符",而不能简单的描述为"多线程共享线程实例变量就是线程安全问题"
自然,如果多线程共享单例,自然也共享单例的状态
下面这样的代码,采用"懒汉"形式的初始化模式,在多线程下,可能会发生错误
如果同时两个线程访问这个单例,一个发现为空,开始建立对象。但是同时在没有建立完的时候,又一个线程进来了,因为上一个线程没有建立完实例,所以第二个实例仍能判断为空。又建立了一个。就不是单例了,单例失败
所以这种"懒汉初始化"只能在单线程下使用。
"懒汉初始化"解决:
同步代码块
同步方法通常开销很大,造成性能低下,因此建议采用同步代码块,但同步代码块存在"二次检查"的问题
下面对初始化代码块加了同步
加入二次检查
或者采用静态块
饿汉初始化根本不会存在线程安全问题,但开销是个问题,因为懒汉模式的使用理念是"用时才初始化",饿汉模式则是"不管用不用都初始化",这本身是和业界流行的资源使用习惯相违背的.
通常的多线程的线程安全问题,往往被描述成"多线程共享线程实例变量"
但多线程下的实例变量如果是单例的话,本来就是该共享的,因为单例在同一JVM下只有一个
这也证明了线程安全的本质是"实际值和理论值不符",而不能简单的描述为"多线程共享线程实例变量就是线程安全问题"
自然,如果多线程共享单例,自然也共享单例的状态
下面这样的代码,采用"懒汉"形式的初始化模式,在多线程下,可能会发生错误
如果同时两个线程访问这个单例,一个发现为空,开始建立对象。但是同时在没有建立完的时候,又一个线程进来了,因为上一个线程没有建立完实例,所以第二个实例仍能判断为空。又建立了一个。就不是单例了,单例失败
单例类 public class MySingleton2 { |
线程类 调用单例类 |
测试代码:,起10个线程,都引用同一实例(单实例多线程) |
执行结果: 看出现多个不同的单例类的实例,违反了单例的规范. com.machome.singleton.MySingleton2@1270b73:19336051 com.machome.singleton.MySingleton2@60aeb0:6336176 com.machome.singleton.MySingleton2@16caf43:23899971 com.machome.singleton.MySingleton2@66848c:6718604 com.machome.singleton.MySingleton2@8813f2:8918002 com.machome.singleton.MySingleton2@1d58aae:30771886 com.machome.singleton.MySingleton2@83cc67:8637543 com.machome.singleton.MySingleton2@e09713:14718739 com.machome.singleton.MySingleton2@de6f34:14577460 com.machome.singleton.MySingleton2@156ee8e:22474382 |
"懒汉初始化"解决:
- 法1.用同步锁保护,但会造成性能低下。而且存在"二次检查"的问题.
public synchronized static MySingleton3 getInstance() throws InterruptedException { |
执行:问题解决, com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 |
同步代码块
同步方法通常开销很大,造成性能低下,因此建议采用同步代码块,但同步代码块存在"二次检查"的问题
下面对初始化代码块加了同步
public static MySingleton3 getInstance() throws InterruptedException { |
执行: 仍出现多个实例 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@60aeb0:6336176 com.machome.singleton.MySingleton3@16caf43:23899971 com.machome.singleton.MySingleton3@66848c:6718604 com.machome.singleton.MySingleton3@8813f2:8918002 com.machome.singleton.MySingleton3@1d58aae:30771886 com.machome.singleton.MySingleton3@83cc67:8637543 com.machome.singleton.MySingleton3@e09713:14718739 com.machome.singleton.MySingleton3@de6f34:14577460 com.machome.singleton.MySingleton3@156ee8e:22474382 |
|
执行: 终于正确了 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 com.machome.singleton.MySingleton3@1270b73:19336051 |
- 法2.采用"饿汉初始化"
|
|