这篇文章主要讲解了“web单例模式优化代码是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“web单例模式优化代码是什么”吧!
public class Singleton1 {
private static Singleton1 instance = null;
private Singleton1() {
}
public static Singleton1 getInstances() throws InterruptedException {
if (instance == null) { //为了优化,当instance不为空是,直接返回实例对象
synchronized (Singleton1.class) {
if (instance == null) {
Thread.sleep(100);
instance = new Singleton1();
}
return instance;
}
}
return instance;
}
}
感谢各位的阅读,以上就是“web单例模式优化代码是什么”的内容了,经过本文的学习后,相信大家对web单例模式优化代码是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是天达云,小编将为大家推送更多相关知识点的文章,欢迎关注!