BonceCP多线程测试...让主线程等待所有子线程退出
2012-08-21 15:43
197 查看
1. 计数方法,每次run完一个线程,计数器++,比较总数和计数器大小即可知道...
2. CountDownLatch计数, 想法和上面类似,只是用封装好的东西实现而已...
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
相关文章推荐
- Java父线程(或是主线程)等待所有子线程退出
- Java父线程(或是主线程)等待所有子线程退出的实例
- Java主线程等待所有子线程执行完毕再执行解决办法
- Java多线程--让主线程等待所有子线程执行完毕
- Java多线程--让主线程等待所有子线程执行完毕(转)
- java主线程等待所有子线程执行完毕在执行(常见面试题)
- Java多线程--让主线程等待所有子线程执行完毕代码
- Java主线程等待所有子线程执行完毕再执行解决办法(转)
- [置顶] [转]Java多线程--让主线程等待所有子线程执行完毕
- 主线程等待所有子线程完成后再执行
- java中主线程等待所有子线程结束
- 主线程等待所有子线程完成后再执行
- 并发问题和主线程等待所有子线程运行完毕再执行
- Java多线程--让主线程等待所有子线程执行完毕在执行
- 主线程等待所有子线程完成后再执行
- 主线程中使用join等待所有子线程执行结束
- 主线程等待所有子线程完成后再执行
- 主线程等待所有子线程执行完毕例子
- Java多线程--让主线程等待所有子线程执行完毕
- Java多线程--让主线程等待所有子线程执行完毕