java 中的单例类(singleton)
单例类是一种设计模式,确保一个类只有一个实例,并提供一个全局访问点。
单例模式的核心特点
- 唯一实例:类只能创建一个对象实例
- 全局访问:提供全局访问点获取该实例
- 自行实例化:类自己负责创建自己的实例
- 构造器私有:防止外部通过 new 创建实例
单例模式的实现方式
1. 饿汉式(eager initialization)
public class eagersingleton { // 类加载时就创建实例 private static final eagersingleton instance = new eagersingleton(); // 私有构造器 private eagersingleton() { // 防止反射攻击 if (instance != null) { throw new runtimeexception("单例模式禁止反射创建实例"); } } // 全局访问点 public static eagersingleton getinstance() { return instance; } public void showmessage() { system.out.println("饿汉式单例"); } }
优点:简单、线程安全
缺点:如果实例未被使用,会造成内存浪费
2. 懒汉式(lazy initialization)
public class lazysingleton { private static lazysingleton instance; private lazysingleton() {} // 线程不安全版本 public static lazysingleton getinstance() { if (instance == null) { instance = new lazysingleton(); } return instance; } }
3. 线程安全的懒汉式
public class threadsafesingleton { private static volatile threadsafesingleton instance; private threadsafesingleton() {} // 方法同步(性能较差) public static synchronized threadsafesingleton getinstance() { if (instance == null) { instance = new threadsafesingleton(); } return instance; } }
4. 双重检查锁(double-checked locking)
public class doublecheckedsingleton { // 使用 volatile 保证可见性和禁止指令重排序 private static volatile doublecheckedsingleton instance; private doublecheckedsingleton() {} public static doublecheckedsingleton getinstance() { if (instance == null) { // 第一次检查 synchronized (doublecheckedsingleton.class) { if (instance == null) { // 第二次检查 instance = new doublecheckedsingleton(); } } } return instance; } }
5. 静态内部类(推荐使用)
public class innerclasssingleton { private innerclasssingleton() { // 防止反射攻击 if (singletonholder.instance != null) { throw new runtimeexception("单例模式禁止反射创建实例"); } } // 静态内部类在第一次被引用时才会加载 private static class singletonholder { private static final innerclasssingleton instance = new innerclasssingleton(); } public static innerclasssingleton getinstance() { return singletonholder.instance; } public void showmessage() { system.out.println("静态内部类单例"); } }
优点:懒加载、线程安全、性能好
6. 枚举单例(最安全的方式)
public enum enumsingleton { instance; public void showmessage() { system.out.println("枚举单例"); } // 可以添加其他方法 public void dosomething() { system.out.println("执行某些操作"); } } // 使用方式 enumsingleton.instance.showmessage();
优点:
- 绝对防止多次实例化
- 自动支持序列化机制
- 防止反射攻击
单例模式的应用场景
// 1. 配置管理器 public class configurationmanager { private static class holder { static final configurationmanager instance = new configurationmanager(); } private properties config; private configurationmanager() { // 加载配置文件 config = new properties(); try { config.load(getclass().getresourceasstream("/config.properties")); } catch (ioexception e) { throw new runtimeexception("加载配置文件失败", e); } } public static configurationmanager getinstance() { return holder.instance; } public string getproperty(string key) { return config.getproperty(key); } } // 2. 数据库连接池 public class databaseconnectionpool { private static final databaseconnectionpool instance = new databaseconnectionpool(); private list<connection> connections; private databaseconnectionpool() { // 初始化连接池 connections = new arraylist<>(); // ... 创建数据库连接 } public static databaseconnectionpool getinstance() { return instance; } public connection getconnection() { // 从连接池获取连接 return connections.isempty() ? null : connections.remove(0); } public void releaseconnection(connection conn) { connections.add(conn); } } // 3. 日志记录器 public class logger { private static volatile logger instance; private logger() { // 初始化日志系统 } public static logger getinstance() { if (instance == null) { synchronized (logger.class) { if (instance == null) { instance = new logger(); } } } return instance; } public void log(string message) { system.out.println("[log] " + new date() + ": " + message); } }
单例模式的注意事项
1. 序列化问题
public class serializablesingleton implements serializable { private static final long serialversionuid = 1l; private static serializablesingleton instance = new serializablesingleton(); private serializablesingleton() {} public static serializablesingleton getinstance() { return instance; } // 防止反序列化创建新实例 protected object readresolve() { return getinstance(); } }
2. 反射攻击防护
public class reflectionsafesingleton { private static reflectionsafesingleton instance; private static boolean initialized = false; private reflectionsafesingleton() { synchronized (reflectionsafesingleton.class) { if (initialized) { throw new runtimeexception("单例模式禁止反射创建实例"); } initialized = true; } } public static reflectionsafesingleton getinstance() { if (instance == null) { synchronized (reflectionsafesingleton.class) { if (instance == null) { instance = new reflectionsafesingleton(); } } } return instance; } }
3. 克隆防护
public class clonesafesingleton implements cloneable { private static final clonesafesingleton instance = new clonesafesingleton(); private clonesafesingleton() {} public static clonesafesingleton getinstance() { return instance; } // 防止克隆 @override protected object clone() throws clonenotsupportedexception { throw new clonenotsupportedexception("单例模式禁止克隆"); } }
总结
实现方式 | 线程安全 | 懒加载 | 性能 | 推荐度 |
---|---|---|---|---|
饿汉式 | ✅ | ❌ | 好 | ★★★ |
懒汉式(同步) | ✅ | ✅ | 差 | ★★ |
双重检查锁 | ✅ | ✅ | 好 | ★★★★ |
静态内部类 | ✅ | ✅ | 好 | ★★★★★ |
枚举 | ✅ | ❌ | 好 | ★★★★★ |
最佳实践建议:
- 如果需要懒加载:使用静态内部类方式
- 如果不需要懒加载:使用枚举方式(最安全)
- 避免使用简单的懒汉式(线程不安全)
- 考虑序列化、反射、克隆等安全问题
到此这篇关于java 中的单例类(singleton)应用场景分析的文章就介绍到这了,更多相关java单例类内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论