试题
考点

设计模式-单例模式-单例模式

面5笔5

编程实现懒汉类型的单例模式

前往“校招VIP”小程序,刷题更快
最新校招难题刷题,快来进刷题群吧
解答

正确答案是

class SingletonLazy {

private static SingletonLazy singletonLazy;

private SingletonLazy() {

}

public static SingletonLazy getInstance() {
try {
if (null == singletonLazy) {
// 模拟在创建对象之前做一些准备工作
Thread.sleep(1000);
synchronized (SingletonLazy.class) {
if(null == singletonLazy) {
singletonLazy = new SingletonLazy();
}
}
}
} catch (InterruptedException e) {
// TODO: handle exception
}
return singletonLazy;
}
}


评论

喜欢你喜欢你

2021-09-13 12:55:00

0 0

卡卡卡乐星

2021-09-12 21:15:00

0 0

麦兜兜麦

2021-09-11 09:55:00

0 0

加载更多