`
臻是二哥
  • 浏览: 183396 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
博客专栏
Group-logo
Java技术分享
浏览量:0
社区版块
存档分类
最新评论

JAVA并发- 典型连接池的实现

阅读更多
package com.xyz.connpool;

public interface IConnection {
	/**
	 * 关闭当前连接
	 */
	public void close();
	/**
	 * 销毁当前连接
	 */
	public void destroy();
	//应该具备的其他方法
}


public class Connection implements IConnection {
	private String name;
	private IConnPool connPool;
	public Connection(IConnPool connPool,String name){
		this.connPool=connPool;
		this.name=name;
	}
	@Override
	public void close() {
		this.connPool.releaseConn(this);
	}
	@Override
	public void destroy() {
		System.out.println("destroy connection---"+name);
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		super.toString();
		return name;
	}

}


public interface IConnPool {
	 /**
	  * 获得一个可用连接,超过最大连接数时线程等待,直到有有连接释放时返回一个可用连接或者超时返回null 
	  * @param maxWaitTime
	  * @return
	  */
	public IConnection getConn();
	/**
	 * 将释放的空闲连接加入空闲连接池
	 * @param conn
	 */
	public void releaseConn(IConnection conn);
	/**
	 * 销毁连接池
	 */
	public void destroy();
	/**
	 * 获取当前线程对应的连接
	 * @return
	 */
	public IConnection getCurrentConn();
}


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ConnPool implements IConnPool {
	private int initNum;
	private int maxNum;
	private int hasAlready;
	private long maxWaitTime;
	private final List<IConnection> freeConnList;
	private final List<IConnection> activeConnList;
	private static ThreadLocal<IConnection> threadLocalVar=new ThreadLocal<IConnection>(){
		protected IConnection initialValue() {
			return null;
		};
	};
	public ConnPool(){
		this.initNum=3;
		this.maxNum=5;
		this.hasAlready=0;
		this.maxWaitTime=1000;
		this.freeConnList=new ArrayList<IConnection>(maxNum);
		this.activeConnList=new ArrayList<IConnection>(maxNum);
		init();
	}
	public ConnPool(int initNum,int maxNum,long maxWaitTime){
		this.initNum=initNum;
		this.maxNum=maxNum;
		this.hasAlready=0;
		this.maxWaitTime=maxWaitTime;
		this.freeConnList=new ArrayList<IConnection>(maxNum);
		this.activeConnList=new ArrayList<IConnection>(maxNum);
		init();
	}
	private void init(){
		for(int i=0;i<this.initNum;i++){
			synchronized (this) {
				this.freeConnList.add(new Connection(this,UUID.randomUUID().toString()));
				hasAlready++;
			}
		}
	}
	@Override
	public synchronized IConnection getConn()  {
			IConnection conn=null;
			if(!this.freeConnList.isEmpty()){
				conn=this.freeConnList.remove(0);
				if(conn!=null)
					threadLocalVar.set(conn);//为线程绑定连接
				this.activeConnList.add(conn);
				return conn;
			}
			if(hasAlready<maxNum){
				conn = new Connection(this,UUID.randomUUID().toString());
				hasAlready++;
				if(conn!=null)
					threadLocalVar.set(conn);//为线程绑定连接
				this.activeConnList.add(conn);
				return conn;
			}
			try {
				this.wait(maxWaitTime);
				if(!this.freeConnList.isEmpty()){
					conn=this.freeConnList.remove(0);
					if(conn!=null)
						threadLocalVar.set(conn);//为线程绑定连接
					this.activeConnList.add(conn);
					return conn;
				}
				return null;
			} catch (InterruptedException e) {
				e.printStackTrace();
				return null;
			}
	}
	@Override
	public synchronized void releaseConn(IConnection conn) {
		if(this.activeConnList.contains(conn)){
			this.freeConnList.add(conn);
			this.activeConnList.remove(conn);
			threadLocalVar.remove();
			this.notify();
                 }
	}
	@Override
	public synchronized void destroy() {
		IConnection conn=null;
		int temp=this.freeConnList.size();
		for(int i=0;i<temp;i++){
			conn=this.freeConnList.remove(0);
			conn.destroy();
		}
		temp=this.activeConnList.size();
		for(int i=0;i<temp;i++){
			conn=this.activeConnList.remove(0);
			conn.destroy();
		}
		conn=null;
		this.hasAlready=0;
	}
         @Override
	public IConnection getCurrentConn() {
		return threadLocalVar.get();
	}
	
}



package com.xyz.connpool;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ConnPoolManager {
	private int initConnNum;
	private int maxConnNum;
	private long maxWaitTime;
	// 连接池存放
	public Map<Integer,IConnPool> pools=new ConcurrentHashMap<Integer, IConnPool>();
//	public Hashtable<Integer,IConnPool> pools = new Hashtable<Integer, IConnPool>();
	
	public ConnPoolManager(int initConnNum,int maxConnNum, long maxWaitTime){
		this.initConnNum=initConnNum;
		this.maxConnNum=maxConnNum;
		this.maxWaitTime=maxWaitTime;
		init();
	}
	
	// 初始化所有的连接池
	private void init(){
		for(int hostId =0;hostId<3;hostId++){
			ConnPool connPool=new ConnPool(this.initConnNum,this.maxConnNum, this.maxWaitTime);
			if(connPool != null){
				pools.put(hostId,connPool);
				System.out.println("Info:Init connPool successed for hostId ->" +hostId);
			}
		}
	}
	
	/**
	 * 根据主机id获取连接池
	 * @param hostId
	 * @return
	 */
	public IConnPool getPool(int hostId){
		IConnPool pool = null;
		if(pools.containsKey(hostId)){
			 pool = pools.get(hostId);
		}
		return pool;
	}
	
	/**
	 * 获取一个到主机hostId的连接
	 * @param hostId
	 * @return
	 */
	public IConnection getConn(int hostId){
		IConnPool connPool = getPool(hostId);
		if(connPool==null){
			System.out.println("Error:Can't find this connecion pool for hostId->"+hostId);
			return null;
		}
		return connPool.getConn();
	}
	
	/**
	 * 关闭指定主机的某个链接
	 * @param hostId
	 * @param conn
	 */
	public void close(int hostId,IConnection conn){
			IConnPool pool = getPool(hostId);
			if(pool != null)
				pool.releaseConn(conn);
	}
	/**
	 * 注销某个主机的连接池
	 * @param poolName
	 */
	public void destroy(int hostId){
		IConnPool pool = getPool(hostId);
		if(pool != null){
			pool.destroy();
		}
		pools.remove(pool);
	}
	/**
	 * 注销所有连接池
	 */
	public void destory(){
		for(int hostId:pools.keySet()){
			this.destroy(hostId);
		}
	}
}



package com.xyz.connpool;
import java.util.concurrent.CountDownLatch;

public class Demo {
	public final static int REQUESTNUM=200;
	public final static int DURATION=20;
	 
	public static void main(String [] args) throws Exception{
		final CountDownLatch startGate=new CountDownLatch(1);
		final CountDownLatch endGate=new CountDownLatch(REQUESTNUM);
		
		final ConnPoolManager cpm=new ConnPoolManager(5, 10, 1000);
		final IConnection conns[] =new IConnection[REQUESTNUM];
		for(int i=0;i<REQUESTNUM;i++){
			final int flag=i;
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						startGate.await();
					} catch (InterruptedException e1) {}
					
					conns[flag]=cpm.getConn(0);
					System.out.println(conns[flag]);
					try {
						Thread.sleep(DURATION);
					} catch (InterruptedException e) {}
					cpm.close(0, conns[flag]);
					
					endGate.countDown();
				}
			}).start();
		}

		long begin = System.currentTimeMillis();
		startGate.countDown();
		endGate.await();
		long over = System.currentTimeMillis();
		System.out.println(over-begin);
//	    Thread.sleep(5000);
	    cpm.destory();
	}
}



源代码下载https://github.com/ZhenShiErGe/ConnectionPool.git
0
2
分享到:
评论

相关推荐

    JAVA上百实例源码以及开源项目

    百度云盘分享 ... Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText();...

    JAVA上百实例源码以及开源项目源代码

     Java实现的FTP连接与数据浏览程序,实现实例化可操作的窗口。  部分源代码摘录:  ftpClient = new FtpClient(); //实例化FtpClient对象  String serverAddr=jtfServer.getText(); //得到服务器地址  ...

    JAVA程序设计教程

    Java程序.............................................................................................6 1.3.1 Java程序的结构 ...........................................................................

    JDBC开发基础

    主要包括JDBC的基本应用开发、传统数据库连接池技术的应用、流行的druid数据库连接池、数据库事务和并发控制、应用程序典型三层架构和DAO层的封装以及最后运用三层架构的框架进行JDBC的综合应用。从零讲起,实战为主...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    为了增加系统的吞吐量,提高并发处理客户请求数量,系统采用了IBM服务器作为主机。在数据库处理方面,不需要在数据层借助存储过程及数据库服务器端函数封装过多的业务逻辑,因此数据库系统采用相对精巧的MySQL[6]。...

    ibatis 开发指南(pdf)

    使用ibatis 提供的ORM 机制,对业务逻辑实现人员而言,面对的是纯粹的Java 对象, 这一层与通过Hibernate 实现ORM 而言基本一致,而对于具体的数据操作,Hibernate 会自动生成SQL 语句,而ibatis 则要求...

    二十三种设计模式【PDF版】

    GoF 的《设计模式》是所有面向对象语言(C++ Java C#)的基础,只不过不同的语言将之实现得更方便地使用。 GOF 的设计模式是一座"桥" 就 Java 语言体系来说,GOF 的设计模式是 Java 基础知识和 J2EE 框架知识之间一...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    说明:用于连接到oracle数据库,也可实现用户的切换 用法:conn 用户名/密码 [as sysdba/sysoper] 注意:当用特权用户连接时,必须带上sysdba或sysoper 例子: 3. 断开连接(disc) 说明:断开与当前数据库的连接 ...

Global site tag (gtag.js) - Google Analytics