/**
* 单例模式。
* @author Bright Lee
*/
public class Singleton {
private static volatile Singleton instance;
private Singleton() {
System.out.println("构造方法被调用了,当前时间戳是:" +
System.currentTimeMillis());
}
public static Singleton getInstance() {
if (instance == null) {
synchronized(Singleton.class) {
if (instance == null) {
instance = new LazySingleton();
}
}
}
return instance;
}
public static void main(String[] args) {
// 并发调用100遍,构造方法只会被调用一次:
for (int i = 0; i < 100; i++) {
new Thread() {
public void run() {
Singleton.getInstance();
}
}.start();
}
}
}
榴芒客服系统:https://blog.youkuaiyun.com/look4liming/article/details/83146776
977

被折叠的 条评论
为什么被折叠?



