자바 개체 풀 기술

다음은 프로젝트 의 코드 간소화 판 입 니 다. 달인 들 의 견 해 를 살 펴 보고 좋 은 코드 를 올 려 주 십시오.질문 장 이 아 닙 니 다. 질문 중 에 이동 하지 마 세 요.하지만 common - pool 패키지 의 대상 탱크 기술 은 비교적 전면적 이다.

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);
	}
}



좋은 웹페이지 즐겨찾기