자바 개체 풀 기술
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);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.