数据库连接池示例以及性能分析

直接上代码:

连接池ConnectionPool:

package com.shuaicenglou.ConnectionPool;

import java.util.LinkedList;

public class ConnectionPool {
	private LinkedList<Connection> pool = new LinkedList<>();
	public ConnectionPool(int ininum){
		if(ininum>0){
			for(int i=0;i<ininum;i++) pool.addLast(new Connection(Integer.toString(i+1)));
		}
	}
	public void relese(Connection connection){
		if(connection!=null){
			synchronized (pool) {
				pool.addLast(connection);
				pool.notifyAll();
			}
		}
	}
	public Connection getConnection(long millis){
		synchronized (pool) {
			if(!pool.isEmpty()){
				return pool.removeLast();
			}else{
				long future = System.currentTimeMillis()+millis;
				long remaning = millis;
				while(pool.isEmpty()&&remaning>0){
					try {
						pool.wait(remaning);
						remaning = future - System.currentTimeMillis();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				Connection result = null;
				if(!pool.isEmpty()) result = pool.removeLast();
				return result;
			}
		}
	}
}
在超时等待那里,也就是代码的27-34行,我原本是直接使用pool.wait(remaning);没有像<<Java并发编程的艺术>>上面那样使用future-remaining的写法,这导致我的连接池超时等待时间莫名地比书上的短,此处为何使用future-remaining的写法会让连接等待时间长一些,我无法理解,但是我的实现与<<Java并发编程的艺术>>上的实现对比,我的实现在并发程度高的时候劣化不够平缓。

模拟的数据库Connection:其commit方法只是睡眠100毫秒:

package com.shuaicenglou.ConnectionPool;

import java.util.concurrent.TimeUnit;
import java.util.jar.Attributes.Name;

public class Connection {
	private String name;
	public Connection(String name){
		this.name = name;
	}
	public void commit(){
			try {
				TimeUnit.MILLISECONDS.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

	}
}

测试:
package com.shuaicenglou.ConnectionPool;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;


public class Test {
	static ConnectionPool pool = new ConnectionPool(10);
	static CountDownLatch start = new CountDownLatch(1);
	static CountDownLatch end;
	public static void main(String[] args)throws Exception {
		int threadCount = 20;
		end = new CountDownLatch(threadCount);
		int count = 20;
		AtomicInteger got = new AtomicInteger();
		AtomicInteger notGot = new AtomicInteger();
		for(int i=0;i<threadCount;i++){
			Thread thread = new Thread(new R(count, got, notGot),"ConnectionRunnerThread");
			thread.start();
		}
		start.countDown();
		end.await();
		System.out.println("total invoke:"+(threadCount * count));
		System.out.println("got connection:"+got);
		System.out.println("not got:"+notGot);
	}
	static class R implements Runnable{
		int count;
		AtomicInteger got,notGot;
		public R(int count,AtomicInteger got,AtomicInteger notGot) {
			this.count = count;
			this.got = got;
			this.notGot = notGot;
		}
		@Override
		public void run() {
			try {
				start.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			for(int i=0;i<count;i++){
				Connection c = pool.getConnection(1000);
				if(c!=null){
					c.commit();
					got.incrementAndGet();
					pool.relese(c);
				}else notGot.incrementAndGet();
			}
			end.countDown();
		}
	}
}
结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值