73

单例的实现方式-姑苏城

 6 years ago
source link: http://blog.51cto.com/ji123/2082775
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
单例模式的实现方式:单例模式要求程序中类只有一个对象。所以我们要将他的构造函数设为private,并提供一个生成对象的静态方法。第一种实现方式:  由于第一种方式是线程不安全的。因为当在if(instace==null)这句话中出现了中断,期间,又有另一个进程B进入,创建了一个实例,而线程A重新获取处理器,又创建了该类的另一个实例。第二种实现方式 这里直接在内部就生成

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK