十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章给大家分享的是有关java线程池如何读取单个SQL数据库表的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
成都创新互联公司成立于2013年,我们提供高端重庆网站建设公司、成都网站制作公司、成都网站设计、网站定制、营销型网站建设、小程序设计、微信公众号开发、seo优化服务,提供专业营销思路、内容策划、视觉设计、程序开发来完成项目落地,为成都茶楼设计企业提供源源不断的流量和订单咨询。
任务:基于线程池来操作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(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(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 + "执行完毕---------"); } }
感谢各位的阅读!关于“java线程池如何读取单个SQL数据库表”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!