Python 官方文档:入门教程 => 点击学习
目录一、饿汉式(静态常量)二、饿汉式(静态代码块)三、懒汉式(线程不安全)四、懒汉式(线程安全,同步方法)五、懒汉式(线程不安全,同步代码块)六、双重检查( DCL )七、静态内部类
public class Face {
private static final Face instance = new Face();
static Face getInstance() {
return instance;
}
private Face() {
}
}
public class Face {
private static Face instance;
static {
instance = new Face();
}
private Face() {
}
public static Face getInstance() {
return instance;
}
}
public class Face {
private static Face instance;
private Face() {
}
public static Face getInstance() {
if (null == instance) {
instance = new Face();
}
return instance;
}
}
public class Face {
private static Face instance;
private Face() {
}
public static synchronized Face getInstance() {
if (null == instance) {
instance = new Face();
}
return instance;
}
}
public class Face {
private static Face instance;
private Face() {
}
public static Face getInstance() {
if (null == instance) {
synchronized (Face.class) {
instance = new Face();
}
}
return instance;
}
}
public class Face {
private static volatile Face instance;
private Face() {
}
public static Face getInstance() {
if (null == instance) {
synchronized (Face.class) {
if (null == instance) {
instance = new Face();
}
}
}
return instance;
}
}
public class Face {
private static volatile Face instance;
private Face() {
}
private static class FaceInstance {
private static final Face faceInstance = new Face();
}
public static Face getInstance() {
return FaceInstance.faceInstance;
}
}
可以减少系统内存开支,减少系统性能开销,避免对资源的多重占用、同时操作
到此这篇关于Java设计模式之单例模式简介的文章就介绍到这了,更多相关Java单例模式简介内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java设计模式之单例模式简介
本文链接: https://www.lsjlt.com/news/124721.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0