1.单例模式:
class SimpleInstance {
//volatile变量仍然有工作内存的拷贝,但是由于它特殊的操作顺序性规定,所以看起来如同直接在主内存中读写访问一般
private static volatile SimpleInstance instance = null;
public static SimpleInstance getInstance() {
if (instance == null) {
synchronized (SimpleInstance.class) {
if (instance == null) {
instance = new SimpleInstance();
}
}
}
return instance;
}
}
网友评论