数据库连接池和多线程

在主线程中,连接池的执行效率低于多线程,但当单独执行时,连接池表现出更高的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package myJDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TestConnectionpool {

	public static void main(String[] args) {
		long time1=System.currentTimeMillis();
		Thread[] t1=new Thread[100];
		Connectionpool pool=new Connectionpool(10);
		String sql="insert into hero value(NULL,'hero',100,100)";
		for(int i=0;i<100;i++) {
			Thread t=new ConnectionpoolThread(pool);
			t1[i]=t;
			t.start();
		}
		
		for(Thread t:t1) {
			try {
				t.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
				
		long time2=System.currentTimeMillis();
		System.out.println(time2-time1);
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		long time3=System.currentTimeMillis();	
		System.out.println(time3-time2);
		Thread[] t2=new Thread[100];
		for(int i=0;i<100;i++) {
			Thread t=new Thread(new Runnable() {
				@Override
				public void run() {
					
					try(Connection c=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8", "root",
							"Wc");
							Statement s=c.createStatement()){
						String str="insert into hero value(NULL,'hero',100,100)";
						s.execute(str);	
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
				}			
			});
			t2[i]=t;
			t.start();
		}
		for(Thread t:t2) {
			try {
				t.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
		long time4=System.currentTimeMillis();
		System.out.println(time4-time3);		
	}
}

class ConnectionpoolThread extends Thread {
	Connectionpool pool;


	public ConnectionpoolThread(Connectionpool pool) {
		this.pool = pool;
		
	}

	@Override
	public void run() {		
		Connection c = pool.getconnection();
		try (Statement s = c.createStatement()){
			String sql="insert into hero value(NULL,'hero',100,100)";
			s.execute(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		pool.add(c);
	}
}

package myJDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

public class Connectionpool {
	int size;
	LinkedList<Connection> list = new LinkedList<>();

	public Connectionpool(int size) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.size = size;
		init();
	}

	public synchronized void init() {
		for (int i = 0; i < size; i++) {
			try {
				Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8",
						"root", "Wc");
				list.add(c);
		//		System.out.println("成功连接" + (i + 1));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void add(Connection c) {
		synchronized (list) {
			list.add(c);
			list.notifyAll();
		}

	}

	public Connection getconnection() {
		synchronized (list) {
			while (list.isEmpty()) {
				try {
					list.wait();// 这里面是this而不是list是有很大的原因的********
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return list.removeLast();
		}
	}
}

执行结果:

280
0
125

可以看出来,在一个主线程里面,连接池的效率比多线程还低!
但是如果分开单独执行,那么连接池的效率比多线程高!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值