时间:2021-05-19
任务:基于线程池来操作MySQL,测试单台机器读写MySQL单表的效率。
思路:创建一个大小合适的线程池,让每个线程分别连接到数据库并进行读取输出操作。
连接到数据库
import java.sql.DriverManager;import java.sql.SQLException; import com.mysql.jdbc.Statement; public class TEXT { } class MySQLOpen { private Connection con = null; private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/phpmyadmin"; private static String username = "root"; private static String password = "root"; private static Statement NULL = null; public void MysqlOpen() { try { Class.forName(driver); //加载驱动类 con = DriverManager.getConnection(url, username, password); //连接数据库 if (!con.isClosed()) System.out.println("***数据库成功连接***"); } catch (ClassNotFoundException e) { System.out.println("找不到驱动程序类,加载驱动失败"); e.printStackTrace(); } catch (SQLException e) { System.out.println("数据库连接失败"); e.printStackTrace(); } }}利用statement类中的executeQuery方法操作MySQL
Statement state = (Statement) con.createStatement();ResultSet sql = state.executeQuery("select * from user where id between 1 and 5");利用sql.next()循环遍历取出想要的数据
while (sql.next()) { String id = sql.getString(1); String username = sql.getString(3); String text = sql.getString(6); System.out.println(id+"\t"+username+"\t"+text);}以上就已经实现了主线程访问并操作数据库的相应内容。
创建线程池,设置好相应参数
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 15, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5));利用for循环去创建线程即可。
计算效率
long start = System.currentTimeMillis();long end = System.currentTimeMillis();System.out.println("平均每秒可输出: " + 100000 / (end - start) + " 条");要注意主线程创建好其他线程后就继续往下执行了,所以要有一个判断其他线程是否结束的语句
while (true) { if (executor.getActiveCount() == 0) break; }可以利用Thread.activeCount()看一还有多少 活跃的线程。
System.out.println("activeCountMain1 : " + Thread.activeCount());主要的思路就再上面,现在贴出整理好的代码:
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit; import com.mysql.jdbc.Statement; public class Main { public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 15, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5)); long start = System.currentTimeMillis(); System.out.println("activeCountMain1 : " + Thread.activeCount()); for (int i = 1; i <= 20; i++) { MySQL mysql = new MySQL(i); executor.execute(mysql); System.out.println("线程池中线程数目:" + executor.getPoolSize() + ",队列中等待执行的任务数目:" + executor.getQueue().size() + ",已执行玩别的任务数目:" + executor.getCompletedTaskCount()); } executor.shutdown(); while (true) { if (executor.getActiveCount() == 0) break; } System.out.println("activeCountMain2 : " + Thread.activeCount()); long end = System.currentTimeMillis(); System.out.println("平均每秒可输出: " + 100000 / (end - start) + " 条"); }} class MySQL implements Runnable { private Connection con = null; private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/phpmyadmin"; private static String username = "root"; private static String password = "root"; private static Statement NULL = null; private final int taskNum; public MySQL(int taskNum) { this.taskNum = taskNum; } public Statement MysqlOpen() { try { Class.forName(driver); //加载驱动类 con = DriverManager.getConnection(url, username, password); //连接数据库 if (!con.isClosed()) System.out.println("***数据库成功连接***"); Statement state = (Statement) con.createStatement(); return state; } catch (ClassNotFoundException e) { System.out.println("找不到驱动程序类,加载驱动失败"); e.printStackTrace(); } catch (SQLException e) { System.out.println("数据库连接失败"); e.printStackTrace(); } return NULL; } @Override public void run() { readMySQL(); } public void readMySQL() { ResultSet sql = null; Statement state = MysqlOpen(); try { sql = state.executeQuery("select * from sina_user_weibos_1386622641 where id between " + ((taskNum - 1) * 5000) + " and " + (taskNum * 5000)); System.out.println("---------task " + taskNum + "正在执行---------"); while (sql.next()) { String id = sql.getString(1); String wid = sql.getString(2); String username = sql.getString(3); String repostscount = sql.getString(4); String commentscount = sql.getString(5); String text = sql.getString(6); String createat = sql.getString(7); String source = sql.getString(15); String lasttime = sql.getString(17); System.out.println(id + "\t" + wid + "\t" + username + "\t" + repostscount + "\t" + commentscount + "\t" + text + "\t" + createat + "\t" + source + "\t" + lasttime); } } catch (SQLException e) { e.printStackTrace(); } finally { try { sql.close(); state.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("---------task " + taskNum + "执行完毕---------"); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Java实现数据库连接池的方法。分享给大家供大家参考。具体如下:packagecom.kyo.connection;importjava.sql.
在开发中中我们经常会使用到数据库连接池,比如dbcp数据库连接池,本章将讲解java连接dbcp数据库库连接池的简单使用。开发工具myeclipse20141、
最近在开发关于java读取ftp中TXT文件,其中有些坑踩了一下,再次做个记录1、读取文件时我会根据文件名称去生成数据库表,oracle数据库对于表名的长度是有
Java线程池ExecutorService1.线程池 1.1什么情况下使用线程池单个任务处理的时间比较短.将需处理的任务的数量大.1.2使用线程池的好处减少在
之前有群友反应同事给了他一个几百MB的sql脚本,导入数据库再从数据库读取数据有点慢,想了解下有没有可以直接读取sql脚本到pandas的方法。解析sql脚本文