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