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

网友投稿 304 2022-12-29


基于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(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

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 = shttp://ql.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 + "执行完毕---------");

}

}

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 = shttp://ql.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 + "执行完毕---------");

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务网关做什么用的(服务网关有哪些)
下一篇:使用接口测试工具测试(接口测试需要的工具)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~