1. 计数方法,每次run完一个线程,计数器++,比较总数和计数器大小即可知道...
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
import com.sun.rowset.CachedRowSetImpl;
public class BoneCPMulti implements Runnable {
// info
private static String clsName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "";
private static String usr = "";
private static String psw = "";
private static String cmd = "select top 10 * from i_task";
private static BoneCP boneCP = null;
private static long count;
private static long times;
private static long time;
private static boolean ok;
private static void init() {
try {
Class.forName(clsName);
} catch (ClassNotFoundException e) {
}
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl(url);
config.setUsername(usr);
config.setPassword(psw);
config.setPartitionCount(3);
config.setMinConnectionsPerPartition(2);
config.setMaxConnectionsPerPartition(500);
try {
boneCP = new BoneCP(config);
} catch (SQLException e) {
}
}
@Override
public void run() {
long start = System.currentTimeMillis();
Connection con = null;
synchronized (BoneCPMulti.class) {
try {
con = boneCP.getConnection();
} catch (SQLException e) {
System.err.println("can not get connection");
}
}
// 创建statement
Statement stmt = null;
if (con != null) {
// System.out.println("connection successful!");
try {
stmt = con.createStatement();
} catch (SQLException e) {
System.err.println("can not create statement");
}
}
// 构建CachedRowSetImpl, 其查询结果不会自动关闭(即使数据库关闭)
CachedRowSetImpl crs = null;
try {
crs = new CachedRowSetImpl();
crs.setCommand(cmd);
crs.execute(con);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
if (crs != null) {
crs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
System.err.println("can not close jdbc");
}
long end = System.currentTimeMillis();
synchronized (BoneCPMulti.class) {
time += (end - start);
++count;
if (count == times) {
System.out.println(times + "\t" + time);
BoneCPMulti.ok = true;
}
}
}
public static void test(int times) {
BoneCPMulti.time = BoneCPMulti.count = 0;
BoneCPMulti.times = times;
BoneCPMulti.ok = false;
for (int i = 0; i < times; ++i) {
new Thread(new BoneCPMulti()).start();
}
}
public static void main(String[] args) {
init();
int t = 1;
System.err.println("times\ttime\n----------------");
for (int i = 0; i < 10; ++i) {
test(t);
t <<= 1;
while (!BoneCPMulti.ok)
;
}
}
}
//times time
//----------------
//1 136
//2 98
//4 372
//8 1258
//16 4534
//32 15640
//64 57999
//128 215699
//256 812172
//512 3291674
2. CountDownLatch计数, 想法和上面类似,只是用封装好的东西实现而已...
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.CountDownLatch;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
import com.sun.rowset.CachedRowSetImpl;
public class MultiBoneCP implements Runnable {
// info
private static String clsName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "";
private static String usr = "";
private static String psw = "";
private static String cmd = "select top 10 * from i_task";
private static BoneCP boneCP = null;
private static long time;
public CountDownLatch sig;
public MultiBoneCP(CountDownLatch sig) {
this.sig = sig;
}
private static void init() {
try {
Class.forName(clsName);
} catch (ClassNotFoundException e) {
}
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl(url);
config.setUsername(usr);
config.setPassword(psw);
config.setPartitionCount(3);
config.setMinConnectionsPerPartition(2);
config.setMaxConnectionsPerPartition(500);
try {
boneCP = new BoneCP(config);
} catch (SQLException e) {
}
}
@Override
public void run() {
long start = System.currentTimeMillis();
Connection con = null;
synchronized (this) {
try {
con = boneCP.getConnection();
} catch (SQLException e) {
System.err.println("can not get connection");
} finally {
notifyAll();
}
}
// 创建statement
Statement stmt = null;
if (con != null) {
// System.out.println("connection successful!");
try {
stmt = con.createStatement();
} catch (SQLException e) {
System.err.println("can not create statement");
}
}
// 构建CachedRowSetImpl, 其查询结果不会自动关闭(即使数据库关闭)
CachedRowSetImpl crs = null;
try {
crs = new CachedRowSetImpl();
crs.setCommand(cmd);
crs.execute(con);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
if (crs != null) {
crs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
System.err.println("can not close jdbc");
}
long end = System.currentTimeMillis();
synchronized (this) {
time += (end - start);
notifyAll();
}
sig.countDown();
}
public static void test(int times) {
MultiBoneCP.time = 0;
CountDownLatch sig = new CountDownLatch(times);
long start = System.currentTimeMillis();
for (int i = 0; i < times; ++i) {
new Thread(new MultiBoneCP(sig)).start();
}
try {
// 等待所有子线程退出
sig.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(times + "\t" + time + "\t"
+ (System.currentTimeMillis() - start));
}
public static void main(String[] args) {
init();
int t = 1;
System.err.println("times\ttimeall\ttime\n----------------");
for (int i = 0; i < 10; ++i) {
test(t);
t <<= 1;
}
}
}
//times timeall time
//----------------
//1 130 131
//2 89 60
//4 348 111
//8 1211 208
//16 4134 421
//32 15819 833
//64 56705 1634
//128 215054 3250
//256 828045 6383
//512 3184355 12759