我估计像我一样,所有人最熟悉的就是单例模式了,尼玛的,就尼玛会这一个,还说不太明白。
单例模式(Singleton)
改善全局变量和命名空间的冲突,可以说是一种改良了的全局变量。这种一个类只有一个实例,且提供一个访问全局点的方式,更加灵活的保证了实例的创建和访问约束。系统中只有一个实例,因此构造方法应该为私有 饿汉式:类加载时直接创建静态实例 懒汉式:第一次需要时才创建一个实例,那么newInstance方法要加同步 饿汉式比懒汉式要好,尽管资源利用率要差。但是不用同步。
public class TestSingleton {
public static void main(String[] args) {
}
}
class ClassA{ //饿汉式
private static ClassA i=new ClassA();
public static ClassA newInstance(){
return i;
}
private ClassA(){}
}
class ClassB{ //懒汉式
private static ClassB i=null;
public static synchronized ClassB newInstance(){
if (i==null) i=new ClassB();
return i;
}
private ClassB(){}
}
单态设计模式(Singleton Design Pattern)
http://developer.51cto.com/art/201208/354428.htm
这篇写的太好了,我一直没有这么深入的学习过单例模式。不过里面有一块没看懂,就是下面这里,待考证,有懂的朋友希望留言告之,不胜感激。
out-of-order write问题,更多相关资料请见: DoubleCheckedLocking:http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
这篇英文文档解释的很清楚。这里是百度文库找到的翻译:http://wenku.baidu.com/view/0335741314791711cc791729.html
在看完上面这篇文章后,有必要看看对volatile的分析
http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166.html
http://developer.51cto.com/art/201105/264855.htm
认真看完了这篇文章, 尽管还有很多不懂的地方,不过大致意思算是懂了,明天继续复习下,我想理解可能会更深一点。
/**
* Snippet 1
*
*/
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton(){
}
public static Singleton getInstance() {
return instance;
}
}
/**
* Snippet 2
*
*/
public class Singleton2 {
private static Singleton2 instance = null;
private Singleton2(){}
public static Singleton2 getInstance() {
if(instance == null){
instance = new Singleton2();
}
return instance;
}
}
/**
* Snippet 3
*/
public class Singleton3 {
private static Singleton3 instance = null;
private Singleton3(){
}
public static synchronized Singleton3 getInstance() {
if(instance == null){
instance = new Singleton3();
}
return instance;
}
}
/**
* Snippet 4
*
*/
public class Singleton4 {
private static Singleton4 instance = null;
private Singleton4(){
}
public static Singleton4 getInstance() {
if(instance == null){
synchronized(Singleton4.class){
if(instance == null)
instance = new Singleton4();
}
}
return instance;
}
}
// 采用Snippet 1 或者Snippet 3 比较安全。Snippet 2和Snippet 4最好在多线程的环境下不要使用,否则可能会出错。
//Works with acquire/release semantics for volatile
//Broken under current semantics for volatile
class Foo {
private volatile Foo helper = null;
public Foo getHelper() {
if (helper == null) {
synchronized (this) {
if (helper == null){
helper = new Foo();
}
}
}
return helper;
}
}
class Helper {
/**
* If perThreadInstance.get() returns a non-null value, this thread has done
* synchronization needed to see initialization of helper
*/
private final ThreadLocal perThreadInstance = new ThreadLocal();
private Helper helper = null;
private Helper(){
}
public Helper getHelper() {
if (perThreadInstance.get() == null){
createHelper();
}
return helper;
}
private final void createHelper() {
synchronized (this) {
if (helper == null){
helper = new Helper();
}
}
// Any non-null value would do as the argument here
perThreadInstance.set(perThreadInstance);
}
}
上面六种形式,值得仔细琢磨。。。