浏览 1383 次
锁定老帖子 主题:项目代码中的连接池,非数据库连接
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-04-08
package com.test; public class Client { private static Client client; private Client() { } public static Client getClient(Client client) { if (client == null) { client = new Client(); } return client; } } package com.test; /** * @copyright * 类说明: 工厂 * @author * @version 创建时间:2011-4-8 */ public class ClientFactory { private static ClientPool clientPool; private ClientFactory(){} public static ClientPool getClientInstance(){ if(clientPool == null){ clientPool = new ClientPool(5,8); } return clientPool; } } package com.test; import java.util.ArrayList; /** * 类说明: 池 * @version 创建时间:2011-4-8 */ public class ClientPool { private Client client=null; private int inUsed=0; //使用的连接数 private ArrayList<Client> freeConnections = new ArrayList<Client>();//容器,空闲连接 private int minConn; //最小连接数 private int maxConn; //最大连接 public ClientPool() { } public ClientPool(int minConn, int maxConn) { this.minConn = minConn; this.maxConn = maxConn; } /** * 用完,释放连接 * @param con */ public synchronized void freeConnection(Client client) { add(false); if(client!=null){ this.freeConnections.add(client);//添加到空闲连接的末尾 System.out.println("释放连接,现有" + inUsed + "个连接在使用!"); } if(this.inUsed==0 && this.freeConnections.size()>this.minConn){ for(int i=this.freeConnections.size()-this.minConn; i>0; i--){ client = (Client) this.freeConnections.get(0); this.freeConnections.remove(0);//如果连接分配出去了,就从空闲连接里删除 System.out.println("释放连接池到最小连接"+this.minConn+",现有" + inUsed + "个连接在使用,连接池数"+this.freeConnections.size()+" !"); } } } /** * * 从连接池里得到连接 * @return */ public synchronized Client getConnection() { Client cl = null; //不能超过最大连接。 if(this.maxConn > this.inUsed){ if (this.freeConnections.size() > 0) { cl = (Client) this.freeConnections.get(0); this.freeConnections.remove(0);//如果连接分配出去了,就从空闲连接里删除 if (cl == null) cl = getNextConnection(); //继续获得连接 } else { cl = Client.getClient(cl); //新建连接 } }else{ System.out.println("达到最大连接数"+maxConn+",现有" + inUsed + "个连接在使用!"); } if (cl != null) { add(true); System.out.println("得到连接,现有" + inUsed + "个连接在使用!"); } return cl; } private Client getNextConnection() { if (this.freeConnections.size() > 0) { client = (Client) this.freeConnections.get(0); this.freeConnections.remove(0);//如果连接分配出去了,就从空闲连接里删除 if (client == null) client = getNextConnection(); //继续获得连接 } else { client = Client.getClient(client); //新建连接 } return client; } private synchronized void add(boolean add){ if(add) this.inUsed++; else if(this.inUsed>0) this.inUsed--; } } package com.test; /** * @copyright * 类说明: * @author * @version 创建时间:2011-4-8 */ public class Test { public static void main(String[] args) { Client clinet1 = ClientFactory.getClientInstance().getConnection(); Client clinet2 = ClientFactory.getClientInstance().getConnection(); Client clinet3 = ClientFactory.getClientInstance().getConnection(); Client clinet4 = ClientFactory.getClientInstance().getConnection(); Client clinet5 = ClientFactory.getClientInstance().getConnection(); Client clinet6 = ClientFactory.getClientInstance().getConnection(); ClientFactory.getClientInstance().freeConnection(clinet4); Client clinet7 = ClientFactory.getClientInstance().getConnection(); Client clinet8 = ClientFactory.getClientInstance().getConnection(); Client clinet9 = ClientFactory.getClientInstance().getConnection(); Client clinet10 = ClientFactory.getClientInstance().getConnection(); System.out.println(clinet1); System.out.println(clinet2); System.out.println(clinet3); System.out.println(clinet4); System.out.println(clinet5); System.out.println(clinet6); System.out.println(clinet7); System.out.println(clinet8); System.out.println(clinet9); System.out.println(clinet10); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |