基于java线程池读取单个SQL数据库表

任务:基于线程池来操作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 + "执行完毕---------");
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。