设计模式-单例模式code
2024-08-31 15:32:11
package singeton; import java.security.SecureRandom; /**
* @author Zero
* @since 2019-08-13.
* Description:
*/
public class HungrySingleton {
private static final HungrySingleton singleton = new HungrySingleton();
private final int ID = new SecureRandom().nextInt(); private HungrySingleton() {
} public static HungrySingleton getSingleton() {
return singleton;
} public int doSomething() {
// System.out.println("I'm HungrySingeton " + ID + "!");
return ID;
}
}
package singeton; import java.security.SecureRandom; /**
* @author Zero
* @since 2019-08-13.
* Description:
*/
public class LazySingleton {
private static LazySingleton singeton = null;
private final int ID = new SecureRandom().nextInt(); private LazySingleton() {
} public static synchronized LazySingleton getSingleton() {
if (singeton == null) {
singeton = new LazySingleton();
}
return singeton;
} public int doSomething() {
// System.out.println("I'm LazySingeton " + ID + "!");
return ID;
} }
最新文章
- MySQL 5.7 学习:新增配置参数
- 网络热恋之XML解析
- [转]AndroidManifest.xml文件详解
- Mac下获取AppStore安装包文件路径
- JavaScript设计模式之策略模式(学习笔记)
- HDOJ 4248 A Famous Stone Collector DP
- stm32之keil开发环境搭建
- Hbase思维导图之数据存储
- 在已有的Java项目中使用Kotlin
- C#中使用Application.AddMessageFilter(this)要手动释放
- 【微服务系列】Spring SpringMVC SpringBoot SpringCloud概念、关系及区别
- 抓取错误之onerror
- spring eureka required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.
- 1084 Broken Keyboard (20 分)
- SDOI 2017 天才黑客
- Bash:常用命令工具-tr命令
- Python全栈开发之15、DOM
- 转CentOS — MySQL备份 Shell 脚本
- windows7系统PC机耳机没有声音
- 省市联动js代码