热爱编程,热爱生活 2019-08-27 11:52 采纳率: 0%
浏览 769
已采纳

多线程读数据库操作,为什么我的代码执行起来比原来还慢呢

/**
* 查询 应用系统 各表的记录总数
*
* @param url 数据源地址
* @param userName 数据源登陆用户名
* @param password 数据源密码
* @param tableNames 表名 List
* @return Map < 表名,表下数据行数>
*/

static class CountThread implements Runnable{
    CountThread(String tableName){
        this.tableName=tableName;
    }

    static void setConnection(Connection connection) {
        CountThread.connection = connection;
    }
    static void setReMap(ConcurrentHashMap map){
        CountThread.reMap=map;
    }
    static void setThreadCount(CountDownLatch countDownLatch) {
        CountThread.countDownLatch = countDownLatch;
    }
    private static CountDownLatch countDownLatch;
    private static ConcurrentHashMap<String,String> reMap;
    private static Connection connection;
    private String tableName;
    @Override
    public void run() {
        String sql = "SELECT COUNT(1) num FROM " + tableName;
        System.out.println(sql);
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            ResultSet tablesRest = stmt.executeQuery();
            while (tablesRest.next()) {
                String count = tablesRest.getString("num");
                reMap.put(tableName, count);
            }
            tablesRest.close();
            tablesRest = null;
            countDownLatch.countDown();
        } catch (SQLException e) {
            log.error(e.getMessage());
            reMap.put(tableName, "0");
        }
    }
}

public static Map<String, String> conutRows(String url, String userName, String password, List<String> tableNames)  {
    int size=tableNames.size();
    Date start=new Date();
    ConcurrentHashMap<String, String> reMap = new ConcurrentHashMap<>(size);
    CountDownLatch countDownLatch=new CountDownLatch(size);
    try (Connection connection = DriverManager.getConnection(url, userName, password)) {
        CountThread.setConnection(connection);
        CountThread.setReMap(reMap);
        CountThread.setThreadCount(countDownLatch);
        for(String tableName:tableNames){
            CountThread thread = new CountThread(tableName);
            thread.run();
        }
        countDownLatch.await();

// sql.append(" SELECT ");
// for (String tableName : tableNames) {
// sql.append(" " + tableName + "b.num " + tableName + "a ,"); //字符串拼接 表名+a 拼接成表别名
// }
//
// //删除多余的一个 ,
// sql.deleteCharAt(sql.length() - 1);
// sql.append(" FROM ");
// //子查询 ,分别统计表字段数
// for (String tableName : tableNames) {
// sql.append(" ( SELECT COUNT(*) num FROM ");
// sql.append(tableName);
// sql.append(" ) " + tableName + "b ,");
// }
// //删除多余的一个 ,
// sql.deleteCharAt(sql.length() - 1);
// System.out.println(sql);
// //使用sql语句进行查询获取结果集
// PreparedStatement stmt = connection.prepareStatement(sql.toString());
// ResultSet tablesRest = stmt.executeQuery();
// //对结果进行遍历,此处结果集实际应只有一行数据
// while (tablesRest.next()) {
// for (String tableName : tableNames) {
// String count = tablesRest.getString(tableName + "a");
// reMap.put(tableName, count);
// }
// }
// tablesRest.close();
// tablesRest = null;
} catch (Exception e) {
log.error(e.getMessage());
throw new RuntimeException("数据源连接失败");
}
Date end=new Date();
long costTime = (end.getTime()-start.getTime());
System.out.println("******************************************"+costTime+"**************************");
return reMap;

}
  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 结构体数组文件读取问题
      • ¥15 写c++代码,第7题说一下使用的计算方法
      • ¥15 postman接口自动化测试报告实践总结
      • ¥15 有关c++的问题,利用相关知识
      • ¥15 求香农编码和解码的matlab代码
      • ¥20 ROS中的TEB局部规划问题
      • ¥20 关于#matlab#的问题:要求测出上面图片中所有同心圆的半径
      • ¥20 epanet软件运行问题
      • ¥15 Python 文件读取
      • ¥60 dpabi进行Alff计算时脑池有干扰信号