package singleton; public class ConnectionPool { private static ConnectionPool pool; private ConnectionPool() { } public static synchronized ConnectionPool getPool() { if (pool == null) { pool = new ConnectionPool(); } return pool; } }
package singleton; public class Application { public static void main(String[] args) { System.out.println(ConnectionPool.getPool()); } }
本文展示了一个简单的单例模式实现的连接池类。通过使用私有构造函数防止外部实例化,并提供一个静态同步方法来获取唯一实例。这种方式确保了在多线程环境下也能正确地创建单一实例。
1236

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



